What is FIND call in request? - rest

I am trying to convert a curl call to python request . The curl call looks like
$ curl -b $COOKIE_FILE \
-X FIND $URL \
-H 'Content-Type: application/json; charset=utf-8' \
I tried using request to get the correct response via requests.post() but i could not get the correct response. Can someone help me with this

Requests allows using arbitrary request methods with the request function, take a look at the documentation.
For example this is how it would look with a Session() object:
import requests
with requests.Session() as sess:
sess.headers = {
#headers in dict format, you could add cookies here too(or set it to sess.cookies)
}
response = sess.request("FIND", url)

Related

Passing parameters in body in POST API Call to Openstack Nova

I am trying to stop a running server instance on Openstack Nova. I am using the following API as a reference
https://developer.openstack.org/api-ref/compute/#stop-server-os-stop-action
The following is my code snippet
import openstack.config
self.cloud_region = openstack.config.OpenStackConfig().get_one('adminproject')
self.session = self.cloud_region.get_session_client('compute')
I am able to make GET requests to the server
response = self.session.get(url)
But when i try to make POST requests, I am getting errors. This is how I am trying to do it
url = "/servers/" + vm_id + "/action
body = {"os-stop":"null"}
reponse = self.session.post(url, {'os-stop' : 'null'})
TypeError: post() takes exactly 2 arguments (3 given)
I have also tried
reponse = self.session.post(url, body=body)
which gives me an error
TypeError: request() got an unexpected keyword argument 'body'
When i do it via the nova client and enable the debug mode, this is what i get
curl -g -i -X POST http://IP:PORT/v2.1/12bf80bc27cf4fdc87a2ce2cb0619159/servers/93bc82d4-7a3b-4f67-bc6e-dfb2553a57fc/action -H "Accept: application/json" -H "Content-Type: application/json" -H "User-Agent: python-novaclient" -H "X-Auth-Token: {SHA1}447e97116c71efcff1a02f4d7fdf8d76a4490d9d" -d '{"os-stop": null}'
It would be great if someone can help in finding out how to emulate the above request.
TIA.

Pass json structure or array data in CURL command to test my REST service

I am looking to test my REST resource on dropwizard using CURL command. I am able to upload the file and get the content and file name. But along with the file I want to pass some list of Ids as well.
How can I pass list of long ids (array of Long data type) to the REST service in the below CURL command?
Can I pass a json structure additionally in the below command if required along with my file content?
CURL command
curl -F 'file=#/cygdrive/c/TestDocument1.txt' http://localhost:8199/test-app/api/upload-documents/1004/documents
REST service to upload file
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Path("/reconciliation-details/{reconciliationDetailId}/documents")
public Response uploadDocument(#FormDataParam("file") File inventoryDocumentContent,
#FormDataParam("file") FormDataContentDisposition fileDetail,
#FormDataParam("reconciliationIds") List<Long> reconciliationIds) throws Exception {
byte[] documentContent = FileUtils.readFileToByteArray(inventoryDocumentContent);
String documentName = fileDetail.getFileName();
reconDetailsService.uploadDocument(documentName, documentContent, reconciliationIds);
return ResponseHelper.createOkResponse();
}
With multipart, each part can have it's own Content-Type header. By default if it is not specified, text/plain is used. Some clients cannot set the Content-Type for individual parts, as mentioned in this post (for which there is a solution mentioned), but cURL has this capability. You just need to append ;type=<content-type>. For example
-F "reconciliationIds=[1, 2, 3, 4];type=application/json"
Instead of the using the actual JSON array, you could also use a file containing the JSON
-F "reconciliationIds=#path-to-json;type=application/json"
Jersey will see that the Content-Type for this part is application/json and use the deserializer that it would normally use for JSON
Make sure to see the linked post above - Even though this may work, you don't want to limit your users to only using clients with this capability. The bottom of the post gives you the solution, though I have not tested it with a List<Long>. I'm not sure how that would work using the getEntityAs(Class), as you wouldn't be able to pass the generic Long type. You could always wrap it in a POJO instead of just using a list.
You need to set your content-type to application/json. But -d sends the Content-Type application/x-www-form-urlencoded, which is not accepted on Spring's side.
Looking at the curl man page, I think you can use -H:
-H "Content-Type: application/json"
Full example:
curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:3000/api/login
(-H is short for --header, -d for --data.)
For Json with file
curl -i -X POST -H "Content-Type: multipart/mixed" -F "blob=#/Users/username/Documents/bio.jpg" -F "metadata={\"edipi\":123456789,\"firstName\":\"John\",\"lastName\":\"Smith\",\"email\":\"john.smith#gmail.com\"};type=application/json" http://localhost:8080/api/v1/user/

Passing token and json as query parameters with curl

I have a URL like below that I use for query
http://<ip>:<port>/api/test/computers?token=a124af&criteria={"and":[["name","=","comp_name"]]}
I was writing a script for this and using curl to download the results from the url. But I am not able to get curl to work. Here is what i tried
curl http://<ip>:<port>/api/test/computers?token=a124af&criteria={"and":[["name","=","comp_name"]]}
-- This runs curl http://:/api/test/computers?token=a124af in background mode.
I know I can pass json data like curl -H "Content-Type: application/json" --data '' . But in this case I have a mix of json as well as other data. How can i achieve that? this does not seem to work
curl -H "Content-Type: application/json" --data "{"and":[["name","=","comp_name"]]}" "http://<ip>:<port>/api/test/computers?token=a124af&criteria="

Https Error in Postman when Send push notification via Rest

I'm fairly new To Parse with Rest Api
I wanna test samples in Their Webistes's ( Here ) in Postman But every time I Post it gives me following Authentication error :
I Wanna test following code in Postman ( see origin here )
curl -X POST \
-H "X-Parse-Application-Id: 0VoIZO8cgmnyfiuklLLkKkxOX7r" \
-H "X-Parse-REST-API-Key: UjU5eb5zjic75mPZVdHExYqnneT" \
-H "Content-Type: application/json" \
-d '{
"deviceType": "ios",
"deviceToken": "0123456789abcdef0123456789cdef0123456789abcdef",
"channels": [
""
]
}' \
https://api.parse.com/1/installations
How Can I resolve this issue ? What is that ? And What is required ?
You are reqeuesting GET request and api doc says its POST request.
1) Enter correct url and select POST request method. Add required headers as per the doc.
2) Enter raw JSON data in your request body.
3) You will receive your output. Since api key and application id is not valid so that I received unauthorized error.

Firefox Add-on RESTClient - How to send data-binary file in the request?

I am trying to use RESTClient Firefox add on to post something equivalent to this:
curl -s -S -X POST -H "Content-type: application/zip" \
--data-binary #./cluster-config.zip \
http://${JOINING_HOST}:8001/admin/v1/cluster-config
The idea is using this Firefox add-on (RESTClient) to debug my stuff until it works. But I am stuck because I don't find the way to add a file to the request (equivalent to --data-binary parameter of curl)
How can I include a file in the request in RestClient Firefox add-on?