I am new to Robot Framework. I was trying to access an URL and generate a token.
I am getting the below error.
Test 1 | FAIL |
405 != 200
upon checking the log
00:52:49.347 INFO POST Response :
url=XXX
status=405, reason=Method Not Allowed
body=None
URL is getting passed correctly and the same is working fine in Postman.
Create Session myssion ${auth_url}
${response}= Post Request myssion /oauth2/token auth=${auth} headers=${headers}
grant_type=${grant_type}
Should Be Equal As Strings ${response.status_code} 200
${token}= evaluate $resp.json().get("access_token")
Please help.
It appears that the problem is a missing space. You only have one space between auth=${auth} and headers=${headers}. You need two or more spaces:
${response}= Post Request myssion /oauth2/token auth=${auth} headers=${headers}
^^
Related
I am trying to query GitLab's API to fetch users.
URL I have passed to the API:
https://gitlab.com/api/scim/v2/groups/cimpress-technology/Users/waad|5cmnhgd#NVJ4bHFYnhfv_mvbj4KGjguJGka_:
The response I get back:
ERROR: Http Error: 401 Client Error: Unauthorized for url:https://gitlab.com/api/scim/v2/groups/XXXXXXX/Users/waad%7C5cmnhgd#NVJ4bHFYnhfv_mvbj4KGjguJGka_:_-c9ef6jS API response we have received: «Response [401]>
I have noticed the | character gets replaced with its URL escape code '%7C' (Refer URL escape code of |)
I have tried multiple ways of URL parsing/encoding like.
import urllib.parse
safe_string = urllib.parse.quote_plus(...)
I am expecting that it should read the '|' character as it is and not use the 7%C url escape code instead.
Is there a way I can avoid this?
Thank you
Client
Python Version - 3.9,
Python Requests module version - 2.25
Server
Java 13,
Tomcat 9.
I have a Tomcat+Java based server exposing REST APIs. I am writing a client in python to consume those APIs. Everything is fine until I send empty body in POST request. It is a valid use case for us. If I send empty body I get 400 bad request error - Invalid character found in method name [{}POST]. HTTP method names must be tokens. If I send empty request from POSTMAN or Java or CURL it works fine, problem is only when I used python as a client.
Following is python snippet -
json_object={}
header = {'alias': 'A', 'Content-Type' : 'application/json', 'Content-Length' : '0'}
resp = requests.post(url, auth=(username, password), headers=header, json=json_object)
I tried using data as well instead of json param to send payload with not much of success.
I captured the wireshark dumps to undertand it further and found that, the request tomcat received is not as per RFC2616 (https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html). Especially the part -
Request-Line = Method SP Request-URI SP HTTP-Version CRLF
Because I could see in from wireshark dumps it looked like - {}POST MY-APP-URI HTTP/1.1
As we can see the empty body is getting prefixed with http-method, hence tomcat reports that as an error.
I then looked at python http library code -client.py. Following are relevant details -
File - client.py
Method - _send_output (starting at line # 1001) - It first sends the header at line #1010 and then the body somewhere down in the code. I thought(I could be wrong here) perhaps in this case header is way longer 310 bytes than body 2 bytes, so by the time complete header is sent on wire body is pushed and hence TCP frames are order in such a way that body appears first. To corroborate this I added a delay of 1 second just after sending header line#1011 and bingo, the error disappeared and it started working fine. Not sure if this is completely correct analysis, but can someone in the know can confirm or let me know how to fix this.
I am calling https://demo.docusign.net/restapi/v2/accounts/{accountId}/envelopes/status?envelope_ids="{\"envelopeIds\":[\"903780a3-cfc4-4dd8-a7b0-90b10d783dc9\",\"8a579670-03ec-411c-b781-1091df7590ad\"]}" with HTTP PUT in c# with httpwebrequest but getting 400 bad request, please help to fix this one issue.
There are two ways to get status of multiple envelopes:
Using GET Call,
GET
/restapi/v2/accounts/{accountId}/envelopes/status?envelope_ids=39eddd06-0288-4288-91a1-dbe79c732524,
5b54c5dd-4c51-4bc1-8251-6e448de34fd4,8f77680d-90fa-4508-9353-0e0707b9518f
no curly braces in the GET call, just comma separated envelopeIds
Using PUT Call,
PUT /restapi/v2/accounts/{accountId}/envelopes/status?envelope_ids=request_body
Body should be -
{
"envelopeIds":["39eddd06-0288-4288-91a1-dbe79c732524",
"5b54c5dd-4c51-4bc1-8251-6e448de34fd4","8f77680d-90fa-4508-9353- 0e0707b9518f"]
}
PUT call is preferred, because using GET there is a limitation on the number of envelopeIds sent in the URL and you might get an error if you hit that limit, whereas in PUT call all envelopeIds are going in the payload so you will not see any error.
file_get_contents(http://smshorizon.co.in/api/sendsms.php?user=***&apikey=******&mobile=*****&message=Hello World&senderid=xxyy&type=txt)
Here in this SMS API when I use a message with space say "Hello World" then it return HTTP/1.1 400 Bad Request. But when I remove the space and instead I write HelloWorld it works fine.
What is the issue?
Why its happening (Problem):
URL is getting spitted because of space between messages and it will consider only part before space.
Solution:
You can use PHP inbuilt function urlencode();
Input : urlencode('Hello World');
Output : urlencode('Hello%20World');
Using cURL I can send a GET request with a body. Example:
curl -i -X GET http://localhost:8081/myproject/someController/l2json -H "content-type: application/json" -d "{\"stuff\":\"yes\",\"listThing\":[1,2,3],\"listObjects\":[{\"one\":\"thing\"},{\"two\":\"thing2\"}]}"
Here is the JSON in a reasonable format for legibility's sake:
{"stuff":"yes",
"listThing":[1,2,3],
"listObjects":[{"one":"thing"},{"two":"thing2"}]}
Normally -d will tell cURL to send a POST but I have confirmed that the -X GET is overriding that and it is sending GET. Is it possible to replicate this with HTTPBuilder?
I have done:
def http = new HTTPBuilder( 'http://localhost:8081/' )
http.post(path:'/myproject/myController/l2json', body:jsonMe, requestContentType:ContentType.JSON) { resp ->
println "Tweet response status: ${resp.statusLine}"
assert resp.statusLine.statusCode == 200
}
Which works, but if I change .post to .get I get the error:
Cannot set a request body for a GET method. Stacktrace follows:
Message: Cannot set a request body for a GET method
Line | Method
->> 1144 | setBody in groovyx.net.http.HTTPBuilder$RequestConfigDelegate
Is there a way to send a GET with a request body using HTTPBuilder?
Short answer: No.
Long answer: The HTTPBuilder doesn't let you set the HTTP method for a request at any time other than when actually creating a request.
Parameters are also set at creating by a closure which checks the type of request and throws that exception if the request is not of type HttpEntityEnclosingRequest.
You can check the source code here: https://fisheye.codehaus.org/browse/gmod/httpbuilder/trunk/src/main/java/groovyx/net/http/HTTPBuilder.java?hb=true
On a side note, the HTTP 1.1 spec doesn't say outright that a GET can't have a body, but it says that if a request semantics doesn't allow one, then it must not be provided, and that servers receiving such requests should ignore it.
Since most people are used to that convention, I would suggest sticking with it, and not having your service actually make use of the body when sending a GET request.
See also this question: HTTP GET with request body