Legacy Dev Forum Posts

 View Only

Sign Up

Cannot make HTTP request to endpoints

  • 1.  Cannot make HTTP request to endpoints

    Posted 06-05-2025 18:24

    Somik_Mukherjee1 | 2022-11-17 15:28:32 UTC | #1

    When I am trying to access the endpoint mentioned below, I get 501 error despite provide correct token:

    url = "https://api.mypurecloud.com/api/v2/teams/{teamsId}/members"

    body = {

    "memberIds": "{userId}"

    }

    headers = {

    "Authorization": "bearer {token}",

    "Transfer-Encoding": "buffered",

    "content-type" : "application/json"}

    req = requests.post(url, data=body, headers=headers)

    print(req)

    Before the response is to try Python SDKs, I would like to clarify the SDKs endpoint is not working for this API and therefore I am trying this route.


    Jerome.Saint-Marc | 2022-11-17 15:47:35 UTC | #2

    Hello,

    The body schema is incorrect.


    memberIds* is an array/list of strings in POST /api/v2/teams/{teamId}/members

    So the body should be something like:

    { "memberIds": [ "idofyour_user" ] }

    You will also have to remove the Transfer-Encoding header. It's that header causing the 501 error.

    Regards,


    Somik_Mukherjee1 | 2022-11-17 16:15:27 UTC | #3

    Jerome.Saint-Marc, post:2, topic:17268
    he Transfe

    Hi Jerome,

    I tried to remove [] in member Id. I removed the transfer-encoding and it resulted in 400 error.

    Thanks, Somik


    Jerome.Saint-Marc | 2022-11-17 16:39:30 UTC | #4

    I don't understand "remove [] in member id". You are supposed to send an array of strings as memberIds. Share what your HTTP request/request body is, what you are sending (hiding/removing token) - otherwise I can't say anything.

    Regards,


    Somik_Mukherjee1 | 2022-11-17 16:41:55 UTC | #5

    400 error came in below code:

    url = "https://api.mypurecloud.com/api/v2/teams/{teamsId}/members"

    body = {

    "memberIds": ["my-user-id"]

    }

    headers = {

    "Authorization": "bearer {token}",

    "content-type" : "application/json"}

    req = requests.post(url, data=body, headers=headers)

    print(req)


    Jerome.Saint-Marc | 2022-11-17 16:49:08 UTC | #6

    If you are literally sending "my-user-id" as the id of a user, this clearly won't work. You are supposed to send a valid userId - i.e. the id of a user in your Genesys Cloud environment


    Somik_Mukherjee1 | 2022-11-17 16:50:08 UTC | #7

    Jerome.Saint-Marc, post:6, topic:17268
    • i.e. the id of a user in you

    The user id is renamed to that text. I am clearly using a valid user id as input. Other masked fields are token and teamsId field.


    Jerome.Saint-Marc | 2022-11-17 17:04:18 UTC | #8

    The HTTP Response - 400 Bad Request has message and code which should give you information on the error.

    If the code is "bad.request", it means something is wrong in your request (body format, or user id not being of GUID format, ...). In this case, I'd suggest that you print your HTTP Request (logs of the sent request to see if something looks incorrect there).

    I have not tried to run your python code. I have just tried to call this endpoint from Postman and from the API Explorer (Developer Tools) and I am not having any issue. It is working fine.

    Regards,


    Somik_Mukherjee1 | 2022-11-17 17:55:08 UTC | #9

    Well the error was indeed "The request could not be understood by the server due to malformed syntax.". After checking a bit more into the request() in python, I altered the request method to the below:

    headers = {

    "Authorization": "bearer token_String",

    "content-type" : "application/json"}

    req = requests.post(url, data=body, json=headers)

    The error now is 401: "message":"No authentication bearer token specified in authorization header.","code":"authentication.required","status":401,"contextId":"58942140-24b7-4124-ba62-886c1e17b467" I have indeed specified the token string in the mentioned manner but is that incorrect?


    tim.smith | 2022-11-17 17:58:20 UTC | #10

    Somik_Mukherjee1, post:9, topic:17268
    json=headers

    That seems very suspect that you're sending the headers as the JSON body payload. Could you try sending the headers using the headers property of requests.post?


    tim.smith | 2022-11-17 18:03:59 UTC | #11

    Somik_Mukherjee1, post:9, topic:17268
    data=body

    This may also be incorrect. All API payloads are JSON, not form data. I believe the JSON body should be set to the json property based on these docs: https://www.w3schools.com/python/ref_requests_post.asp

    Of course you're not required to use the SDK, but it already does all of this for you and provides request functions and classes for each endpoint to reduce this particular kind of effort. Even if you don't want to use the SDK, you can still look at its code and see how it makes requests. https://github.com/MyPureCloud/platform-client-sdk-python/blob/master/build/PureCloudPlatformClientV2/api_client.py#L343


    system | 2022-12-24 08:33:23 UTC | #13

    This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.


    This post was migrated from the old Developer Forum.

    ref: 17268