Search code examples
apipostmanrobotframework

How to solve HTTPError: 400 Client Error: Bad Request for URL inRobot Frame work


I know there are many similar kinds of questions available but none of them worked. Can someone tell me if there is any kind of syntax error for the Testcase below

Create Token
    Create Session      testsession        ${baseUrl}       verify=true
    ${body}=    create dictionary    clientId=unittest.cc.client        clientSecret=RyDQ$xxxxxRtv
    ${header}=  create dictionary    Content-Type=application/json
    ${resp}=    POST On Session    testsession    ${reqUri}    json=${body}    headers=${header}    params=${ApiKeyParameter}
    ${source data}=    Evaluate     json.loads("""${resp.content}""")    json
    ${token}=   Set Variable   ${source data['accessToken']}

   #No errors Uptill this much - Bearer token creation was successful after that getting error while using it

    ${header}=   create dictionary       Authorization=${tpre} ${token}     Content-Type=application/json      cookies=ss-id=KF84fFy4txxxxxxxxx76i; ss-pid=StDTDxxxxxxxxxxxxn7r
    ${body}=    get file    API/data.txt
    log to console    ${header}
    ${resp}=    post on session     testsession     /orders     json=${body}    headers=${header}
    log to console    ${resp.status_code}

The problem is every time I run the test I am getting a 400 error. Below is the Python code provided by POSTMAN and the screenshots of the headers used. Now I am not sure of how to get the HOST header in my python or maybe robot framework. Please let me know if any additional details are needed. I am not sure of headers in the URL formation while get or post request is done Is there any way to find that out?

import requests
import JSON

url = "https://domain:10001/orders?format=json"

payload = json.dumps({ Can ignore this part
})
headers = {
  'Authorization': 'Bearer xxx',
  'Content-Type': 'application/json',
  'Cookie': 'ss-id=xxx; ss-pid=xxx'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

enter image description here


Solution

  • If you all ever come across this kind of issue don't forget to check the body of the JSON / XML you are sending. Mine resolved as I was saving the dump JSON in a text file so while reading from the file my code was adding some extra spaces in front so I was getting a 400 error.

    For further information try logging the Response Content it must show you the error message.