How to upload an image file using Rest Assured framework? - rest-assured

I am trying to upload a file using the rest assured framework. The call is a POST call on the API that i am using and the code is mentioned below:
given()
.contentType("image/jpg")
.accept("application/json")
.auth().oauth2(accessToken, OAuthSignature.QUERY_STRING)
.multiPart(new File("C:/Snap0000.jpg"))
.post("/objects/files")
.getBody();
The error that I get when doing this is:
400 - Unable to read image info Couldn't read magic numbers to guess format.
What am I doing wrong?

You may be missing the control name for the multipart? See javadoc for the multiPart method or look at the documentation.
Edit: above link is (partially) broken; this one at github seems to work better: https://github.com/rest-assured/rest-assured/wiki/Usage#multi-part-form-data

Related

Ruby on Rails basic use of RiotGames API (need explanation, solution already found)

First you must know I'm a total beginner, I'm trying to learn so I almost don't know anything.
On the basic page of the API, there is a curl command used as an example to show us how to make requests.
I'm using Ruby on Rails so I used "curl-to-ruby" website to translate it, but it did not work as expected.
I wanted it to show me this :
uri = URI.parse("REQUEST_URL")
response = JSON.parse(Net::HTTP.get(uri))
Instead I got this :
uri = URI.parse("REQUEST_URL")
response = Net:HTTP.get_response(uri)
I don't understand any of this, I thought I wouldn't need to and just use "curl-to-ruby", but apparently I really need to get this.
Would you please try to explain me ?
Or give me links ?
Or matters to read (curl, API, http) ?
Thank you very much, have a nice day.
It's because that command doesn't return just the content, it returns the whole HTTP response object including headers and body. You need to extract the response body and parse that using JSON.parse(), e.g.
JSON.parse(response.body)
See documentation here: https://docs.ruby-lang.org/en/2.0.0/Net/HTTP.html#method-c-get_response
(Also, there is nothing in the cURL command which would hint to the converter that the content-type of the response was expected to be JSON (e.g. perhaps an "accepts" header or something), so even if it were able to produce extra code adding the JSON.parse part, it has no way of knowing that it would be appropriate to do so in this case.)

Param is missing or the value is empty on POST request to api

I've downloaded this repo (api-presentation) to experiment with api in rails app.
Here's an article with video that explains what is api and how to use it....
http://www.aomran.com/designing--building-restful-json-apis/
Now I'm trying to add a record through POST request with Postman extension and I'm geting:
param is missing or the value is empty: contact
What am I doing wrong?
Here's a screen with the error:
I added Header/Value and it worked for me. The question's probably lame.

iPhone - How to post and get xml data to server?

I want to get xml string from url "http://test.php?value=xxx",here xxx shall be in XML format,for example:<request><command>test</command></request>.After I post this,the server will give me a result with xml string.But i don't know how to do that.Anyone has a solution?Thanks.
http://www.raywenderlich.com/30445/afnetworking-crash-course
That website gives you a step by step exactly what you need with AFNetworking. The question you are asking is very vague. If you get more specific then I can give direct code. Otherwise that crash course website is very informative.

ASP.NET WEB API 406 error:for POST request using Media format

I am very new to web api stuff:
I am getting an error
406: Not Acceptable
error message in asp.net web api rest service.
In my rest service I’m using media format for my customized XML output, to get customized output.
I’m registering my formatted media in Global.asax page.
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new mynewformat());
all my methods are post methods with typed object as parameter and parameters are accepts from body.
Whenever I try to test the service… Getting 406: Not acceptable error message.
can anyone please help me ... what could be the reason for this....???
I did notice couple of interesting points here...
If I’m commenting below line then I’m getting 200 (OK) status code (which is fine.)... but format is not applying to output.
GlobalConfiguration.Configuration.Formatters.Clear();
If i'm removing parameters in my service method.. Then its working
fine..
I request everyone.. Please guide me what could be the reason/work around/solution/fix..for this issue.
Note:I don't want accept parameters from URI so i made it to accept from frombody only.
Thanks.
There is a lot more to implementing a custom format than just adding it to the configuration formatters. It starts with having to change the media-type header to a new custom type of your choosing (like "application/myNewFormat") for all requests, for the client. On the back end, you have to implement a new MediaTypeFormatter that can handle the serialization. This involves a bit more of code.
A good example of this resides here, it can easily be stripped to boiler-plate code:
http://www.codeproject.com/Articles/559378/Implementing-Custom-Media-Formatters-in-ASP-NET-We

Youtube API missing field

I have problem getting statistics information from youtube data api. I make a request to http://gdata.youtube.com/feeds/api/videos?q=video_id&alt=json, it works for some, but for some video id, the response does not contain 'entry', 'yt$statistics', 'gd$rating' for example:
zLcbznigfs missing 'entry', aVfN6XjACDY missing 'yt$statistics', fjhQ9Kf4iHk missing 'gd$rating'
After moving around, i found out the solution for this: use &alt=atom instead of using &alt=json, which means that we better read from Atom feed than JSON (and feedparser is an excellent module for doing this). I have checked this with several video id, it works fine.
Hope that help. Thanks.

Resources