REST how to handle query parameters when put to resource? - html

I have a REST data service where I want to allow the users to create new items with HTTP PUT using different formats like json,xml,csv. I'm unsure how to best handle the format specification in the url:
PUT /ressource/ID/json
PUT /ressource/ID/xml
or
PUT /ressource/ID?format=json
PUT /ressource/ID?format=xml
So what is the best way to specify a format indicator?
If I specify the format with an query parameter and want to do a PUT how can I do this with curl?
curl -T test/data.json -d "format=json" http://localhost:5000/resource/33
does not work.
curl -T test/data.json http://localhost:5000/update?format=json
works, but I would rather let curl build the query parameters instead of adding them by myself.

A general principle of RESTful web services is to use the features built-in to HTTP, when applicable. In this case, you can indicate the format of your PUT request's content by setting the Content-Type header to application/json or application/xml.

Related

Can I utilize cURL parameters without using cURL?

This is probably a stupid question but I'm pretty out of my depth here. I'm trying to utilize an API for my business, and while most of the API has the parameters in the form of "site?param1=one&param2=two", one of them does not. Instead it's in the form
required parameters -> key
optional parameters
params: a key-value array of where clauses for the query
I would love to be able to put the parameters in directly in the link, but I'm not sure how to parse this or if it's possible.
Example
https://thepetresorts.gingrapp.com/api/v1/animals?key=KEY&params=[{animal_id=1},{name=Charlie}]
This is the example they provided using cURL, but I'm really not interested in actually programming with the API, I just need to make specific pulls every once in a while, and I cannot for the life of me figure out how to utilize cURL.
curl "https://{your-subdomain-here}.gingrapp.com/api/v1/animals" \
-H 'Content-Type: application/x-www-form-urlencoded; charset=utf-8' \
--data-urlencode "params[month(from_unixtime(birthday))]=11" \
--data-urlencode "key={your-key-here}"
Any advice would be extremely appreciated!
If you're working with an API, it's even better if you can use POSTMAN or INSOMNIA.
The API your describing has two types of requests, GET and POST - the GET request is usually used to "get to view" but occasionally some use it to also modify and push data - this uses parameters within the URL (what you described), POST however is generally used to push data into the body to make a modification, the parameters are posted within the body of the request instead of in the actual URL like GET requests.
You won't be able to modify the endpoint (unless its your own) you will need to GET where its needed and POST when its needed, the POST request will likely deny a GET request as it's not the intended method - you can create a jump page (your own API) to accept GET parameters and generate a POST request to your API - as #muklis mentioned POSTMAN is great you can create the request in there, generate the code in any language and use that to produce a simple PHP page or so that'll take in $_GET[] variables and pass them into the generated POSTMAN request - it's probably your best bet.
-- Edit
You can also use Zapier, forgot about that - Zapier is amazing for easy code-less integrations; you can use the Webhook zap to receive your parameters in repost them in two easy steps within the zap.
Just another idea for you.

Pentaho HTTP Post using JSON

I'm brand new to Pentaho and I'm trying to do the following workflow:
read a bunch of lines out of a DB
do some transformations
POST them to a REST web service in JSON
I've got the first two figured out using an input step and the Json Output step.
However I have two problems doing the final step:
1) I can't get the JSON formatted how I want. It insists on doing {""=[{...}]} when I just want {...}. This isn't a big deal - I can work around this since I have control over the web service and I could relax the input requirements a bit. (Note: this page http://wiki.pentaho.com/display/EAI/JSON+output gives an example for the output I want by setting no. rows in a block=1 and an empty JSON block name, but it doesn't work as advertised.)
2) This is the critical one. I can't get the data to POST as JSON. It posts as key=value, where the key is the name I specify in the HTTP Post field name (on the 'Fields' tab) and the value is the encoded JSON. I just want to post the JSON as the request body. I've tried googling on this but can't find anyone else doing it, leading me to believe that I'm just approaching this wrong. Any pointers in the right direction?
Edit: I'm comfortable scripting (in Javascript or another language) but when I tried to use XmlHttpRequest in a custom javascript snippet I got an error that XmlHttpRequest is not defined.
Thanks!
This was trivial...just needed to use the REST Client (http://wiki.pentaho.com/display/EAI/Rest+Client) instead of the HTTP Post task. Somehow all my googling didn't discover that, so I'll leave this answer here in case someone else has the same problem as me.
You need to parse the JSON using a Modified JavaScript step. e.g. if the Output Value from the JSON Output is called result and its contents are {"data"=[{...}]}, you should call var plainJSON = JSON.stringify(JSON.parse(result).data[0]) to get the JSON.
In the HTTP Post step, the Request entity field should be plainJSON. Also, don't forget to add a header for Content-Type as application/json (you might have to add that as a constant)

How to specify mime-type in URL to specify JSON from RESTful web service

Can anyone tell me if it is possible to specify mime-type returned from a RESTful web service through the URL? I am trying to demo a simple service that I created to someone and I am just using a web browser (Chrome) to invoke the service. I am trying things like this:
http://localhost:8088/providers?mimeType={application/json}
http://localhost:8088/providers?mimeType=application/json
http://localhost:8088/providers?mimeType=json
None of these work for me - I just keep getting XML returned no matter what I select.
I found several posts related to this subject, but nothing with an answer about how to do this through a URL (I found some which talked about setting headers, but I am using a browser and don't have the ability to specify headers). I found this post:
REST Content-Type: Should it be based on extension or Accept header?
And that post linked to this: http://www.xml.com/pub/a/2004/08/11/rest.html
On the xml.com site, I found this text:
URI-Specified Representation [PS, AR]
A client can specify the representation using the following query string:
mimeType={mime-type}
A REST server should support this query.
So it seems that what I am trying to do should be possible, but I can't figure out how to make it work. Can anyone help?
Thanks.
To define the headers added to your request, you can use a Chrome extension like Postman.
Then you will be able to specify an Accept header to tell Talend to return json:
Accept: Application/json

Do REST POST action from within HTML

Further to https://stackoverflow.com/questions/16726368/apache-user-auth-and-redirection-based-on-remote-user, I need to do some POSTs to the REST API but I'm struggling on how to implement.
I need to do the equivilent of:
/usr/bin/curl -s X POST -H "Accept: application/xml" --cacert ca.cer -u user#domain:password -d "<action />" https://server:port/api/id/stop
in just plain HTML.
Any ideas?
No feature of HTML will allow you to construct a request in which you:
Override the browser's Accept header
Specify an certificate to use instead of the browser's library of them
Specify HTTP auth (at least cross browser, some may still accept URIs in the form http://foo:bar#example.com) or
Make a POST request with XML as the body
You could specify some of that using JavaScript/XHR, but not with "just plain HTML".

Querying REST API using cURL

To begin with I am a novice, both with REST and cURL. This is what I want to do:
I want to query the Twitter Rest API using commandline (some bash script) and hence, I want to use cURL to do that.
I have been trying to search for some tutorial (with no luck) that explains how to use REST with cURL and how to form the headers, get/post and then how to receive the REST response and the response status, so that I can write my logic based on the response status.
I would greatly appreciate if you can give me a few examples on how to call the twitter REST API using curl and also how to access the REST response status.
I tried calling twitter using this:
curl "http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&screen_name=$user_name&count=150"
And got a json which I was able to use in my program. But I am not sure how to access the rest response status codes (in order to make my implementation more reliable). Also, I am not sure about what other flags should I provide to the cURL while calling it.
PS: Just to add, if you think there is some other MUCH better alternative to retrieving data from REST API other that using cURL, then also please let me know. Though my application is not web based and is more data crunching oriented and that's why I chose commandline and bash.
curl -i will print out the header information:
HTTP/1.1 200 OK
Date: Sat, 29 Dec 2012 07:34:00 GMT
I know it doesn't directly answer your question, but I'd suggest using Python instead of bash, for a number of reasons. And if you decide on that, I strongly recommend the marvellous Requests library: http://docs.python-requests.org