Get form parameters from a post request using spray/scala - scala

I'm really new with all this Scala/Spray. With some testing I was able to get parameters from a Get request using the parameters function. However I'm trying to fetch some parameters sent from a POST request on the body of the request. It seems like parameters function is unable to fetch those values.
As an example, I'm trying to get this values "name=john&lastname=smith" from the post request body. What is the best option to get these values?
Thank you

You could use [Form-Field-Filters] to extract parameters from POSTs
[Form-Field-Filters] https://github.com/spray/spray/wiki/Form-Field-Filters

Indeed, the parameters directive only handles things actually in the query-string and not parameters in the body of the request. To get things out of the body, you'll need to use the content directive and then unmarshal the content.
This spray-user thread may be helpful, as it includes some unmarshalling code doing precisely what you're looking for.

As of recent Spray versions, you need to use the Unmarshaller for FormData.

Related

Is there a way to set headers for GET requests in KDB?

I'm trying to make get requested with .Q.hg (HTTP get), but I need to edit the request headers to provide API keys. How can I do this?
You can try this function I wrote a few years back for a POC (similar reason - I needed to supply multiple headers). It's based on .Q.hmb which underpins .Q.hp/hg. Please note - it was never extensively tested & there are likely better alternatives out there, but it will perhaps work as a quick solution.
k)req:{[url;method;hd;bd]d:s,s:"\r\n";url:$[10=#url;url;1_$url];p:{$[#y;y;x]}/getenv`$_:\("HTTP";"NO"),\:"_PROXY";u:.Q.hap#url;t:~(~#*p)||/(*":"\:u 2)like/:{(("."=*x)#"*"),x}'","\:p 1;a:$[t;p:.Q.hap#*p;u]1;(4+*r ss d)_r:(`$":",,/($[t;p;u]0 2))($method)," ",$[t;url;u 3]," HTTP/1.1",s,(s/:("Connection: close";"Host: ",u 2),((0<#a)#,$[t;"Proxy-";""],"Authorization: Basic ",.Q.btoa a),($[#hd;(!hd),'": ",/:. hd;()])),($[#bd;(s,"Content-length: ",$#bd),d,bd;d])}
It takes 4 arguments:
Resource URL
HTTP method
Dictionary of headers
Message body as JSON object
Sending a request to a test server..
q).j.k req["https://httpbin.org/get";`GET;("Content-Type";"someOtherHeader")!(.h.ty`json;"blah");""] // no body so pass empty string
args | (`symbol$())!()
headers| `Content-Type`Host`Someotherheader`X-Amzn-Trace-Id!("application/jso..
url | "https://httpbin.org/get"

Jmeter parameter without name in REST method

Hello i have GET method that URL example is:
http://localhost:8050/programs/b3cb6a0f-5d29-4744-a7e8-5fa0099dab18
Where the last String is just programId that I set as parameter in HTTP Request.
JMeter is kinda confused and the respond in raw request is:
GET http://localhost:8050/
GET data:
b3cb6a0f-5d29-4744-a7e8-5fa0099dab18
but there's just 404 in response data.
I can just delete parameter and write /programs/b3cb6a0f-5d29-4744-a7e8-5fa0099dab18 in path instead of /programs/ and everything works perfectly fine. IMO it's awful way. I'd prefer do it with parameter.
Write in path the parameter as /programs/${programId}.
This is part of the path and not parameter, parameters in GET request are different than POST see http methods
path/mypage?param1=value1&param2=value2

Play 2.2.x: Query parameters in POST body to avoid exceeding URL limit

I have an API route that takes a list which can sometimes be so long that it exceeds the URL length limit for some browsers. It looks like this:
GET /api/test Controller.test(potentiallyLongList: List[Long], other: String, ...)
To avoid this problem we have decided that if the client sees that the list is too long, it will switch to sending the potentiallyLongList parameter in the POST body.
I'm wondering what the best way to do this is? The potentiallyLongList parameter shows up in several routes so I'd like to avoid having two methods for each one.
One thing I thought of is to catch all POST requests, check if they have the list in the body, and then move it to the URL and send it through Play's routing process again. Unfortunately the onRouteRequest handler in Global.scala hasn't parsed the body yet, so I can't do that.
Is there any way in an action to modify the url and then run the full request again? Or is there a better way I can solve this problem?

How to deal with a media-type for error messages of the Cowboy REST handler

I want the user to have the opportunity to choose the format in which it receives a response from the server, whether it is plain text, json or xml. It's looks like I must to retrieve media_type by calling cowboy_req:meta/{2,3} and then use it for encoding a response body. But that value doesn't available in callbacks before content_types_provided (malformed_request, is_authorized, forbidden...).
Should I dublicate a cowboy logic and write my own code to determine media_type?
Or ignore all callbacks which had executed before the media_type has been determined.
Or maybe should I to place my response message into request metadata and encode it in the onresponse hook, then replace response body?
How should I do that?
I think you are not quite wright. Straight from init/3 and rest_init/3 functions the Request parameter is "full request", and you can read any header or meta in each callback.
And personally i would go with Header over Meta (since there is already Content-Type header defined, and Headers should take presence over Meta).
In general the REST callbacks in cowboy should only give you easily understandable workflow for handling request, with additional default response codes. In is_authorized/2 all you need to do is check authorization, simply return true or false (as part of tuple), and cowboy will either move forward with you logic or return 401 code. Checking is someone allowed to make request should not be determined on response format, but still, if you would like to do it, just read this Meta from Req parameter, and return true/false based on it.
And the only difference with content_types_provided/2 is that you return kind of bindings between Content-Types header values and your functions. I think all you need could be based on this official example

parse query paramaters of a random uri in GWT

I am unable to find a reference on how to parse a random uri to access query parameters in GWT. I know Winodw.Location allows parsing current window url, but I want to parse another independent url (Document.get().getReferrer() in fact).
Any advice on how to proceed is very much appreciated.
Thanks.
If you can get components of the URI, use com.google.gwt.http.client.UrlBuilder.
Otherwise, use the "ugly" method described in this Stackoverflow post: https://stackoverflow.com/a/9352299/1038832