RESTserver POST request format - codeigniter

I am developing an API using CodeIgniter and the RestServer for CI (see below). I am also using the Firefox RestClient plugin to test the API.
What I am wondering is how to do the test post (what format).
Tried {"desc":"value"} but it did not work. The API is not "seeing" the incoming post fields.
http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/

the post body doesn't need to have a specific format, but the most convenient is to encode the body in the same way web browsers encode form data, specifically Content-Type: application/x-www-form-urlencoded. In particular, the Host and Content-Length headers are not optional, and the Content-Type header is usually needed to tell the server how to interpret the body. A well formed POST request will look like:
POST /path/to/resource HTTP/1.0
Host: example.com:80
Content-Length: 21
Content-Type: application/x-www-form-urlencoded
key=value&key2=value2
It's still up to the server to recognize the content-type header and parse the body that way.
Note that the data is after all the headers, not as part of the request path (in the first line).

Optionally, you can use Proxy Library which i wrote for CI. With that, you can simulate any of possible call to your API(its works for popular REST API too), with more simple syntax instead using cURL...
// An example call to your API end point using POST, will be simply
$this->load->library('proxy');
$this->proxy->http('POST', 'http://somesite.com/api/users', array('username' => 'foo', 'password' => 'bar'));
You can define whatsoever HTTP header too (like API Key or whatever else).

Related

Handling text/plain request in Laravel (sent via navigator.sendBeacon)

Problem:
I am trying to get the content / body from a text/plain POST request in a Controller in Laravel 8.
Nothing in the docs or numerous google searches worked.
I have tried:
$request->input();
$request->all();
$request->json();
json_encode($request);
All of these seem to show that the request is completely empty.
Context
This may not be relevant to the solution, but might help other's who are trying to google this problem: The request I am trying to handle with my controller is being sent by navigator.sendBeacon on the client side. The request body is actually stringified JSON, but sendBeacon does not allow you to send requests with content-type JSON. Devtools show the content-type header for this request as 'text/plain'.
My Answer:
Use $request->getContent() to get the content of a text/plain HTTP request.
And then, if you are in a situation like mine where the text is actually JSON, use json_decode($request->getContent(),true) to get a standard PHP array from the content which you can use in your Controller.
It turned out to be quite simple, but the information was not in the docs, or in any online searches so I thought it would be worthwhile to post to SO anyways...
You can send the data as JSON like this instead
data = new Blob([JSON.stringify(data)], {type : 'application/json'})
navigator.sendBeacon('/your/route/here', data)

Hot to define a fallback mapping temmplate in AWS Api Gateway?

I have a POST method exposed though an API in AWS API Gateway. It would process a certain JSON. But there is a weird requirement: I am supposed to accept anything and return always a 200 status code (as long as the service is available and working of course).
So if I receive a call with Content-Type: image/jpeg, application/xml or what/ever, I must be able to map the request to my Lambda function.
I have achieved to map any request to a custom type using the VTL (Velocity Template Language), the issue is that I must specify every single Content/Type I want to support in the Integration Request.
I would like to define a fallback mapping template for every Content-Type that is not among those that I have already defined. It is not allowed to specify "*/*" as Content-Type.
I can not use When no template matches the request Content-Type header option as passthrough because b y default the lambda function tries to deserialize the body from a JSON format.
Any ideas?
I think you should enable the When no template matches the request Content-Type header option. As documented your Lambda will get an API Gateway event. This event has a headers field that, on a POST should contain a content-type field. If your code understands the content-type then handle it. If not, return a response that has the correct status code and whatever is appropriate. This is not on the API gateway side but rather on the Lambda side.
See this blog for some additional ideas.

Apache Nifi : How to pass post params and capture response from post API - traditional non rest, non json API

I'm trying to pull data from a post API that expects authentication and request parameters as part of the request body. I guess, what they do is access the data from the post variables.
What I have seen so far in documentation is how to send POST via JSON or headers. In my case, no headers just the post body parameters.
call to this api via curl --data option works just fine.
curl --data "username=xyz&password=xyz&function=xyz" http://example.com/api.php
How can I replicate above call in nifi?
I have tried multiple methods without success. Latest has been Generate flow file, update attributes (where i fill in the parameters), invoke http then putfile.
But I'm getting errors - the api is not abe to authenticate my request.
Thanks
If you need to send the following data in body, then put it into content of your flowfile.
username=xyz&password=xyz&function=xyz
The easiest way to put it into the Custom Text property of the GenerateFlowFile processor.
Usually for this kind of body you have to provide content type header:
content-type: application/x-www-form-urlencoded
If you don't need any additional headers then you don't have to define any additional attributes of the flow file.

Sending an http POST with netcat

I am trying to send data within the body of my http post. The following code makes the POST but I can't figure out how to get data into the body or any other field besides the http version and URL fields. I've looked and tried different things from other sources on the net to no avail. Ultimately this will be within a bash script - I can\t use CURL or WGET.
printf "POST /postresearch HTTP/5.0\r\n\r\nTHIS IS THE BODY" |nc -n -i 1 10.0.1.11 3000
You need to make a valid HTTP request, which includes using the correct protocol version number and for POST requests, providing an entity length, and possibly a content type.
POST /postresearch HTTP/1.0
Content-Length: 25
Content-Type: application/x-www-form-urlencoded
field=value&field2=value2
I issue a HTTP 1.0 request, specify that the body will be 25 bytes, and specify that I'm sending URL-encoded form parameters (this is how browsers send forms when you submit them).
BTW, don't do this. There is absolutely no situation where you should be implementing your own HTTP client. Use a proper HTTP tool or library.

Is it IMPOSSIBLE to POST JSON with XDomainRequest?

As I am not able to find a way to set the content-type to JSON for the data I sent via XDomainRequest,
I naturally ask:
Is it IMPOSSIBLE to POST JSON with XDomainRequest?
"Only text/plain is supported for the request's Content-Type header"
Looking at some other questions, I'd say this is possible.
For example: How to send JSON data in XDR using the POST method
You can still POST the JSON as plain text and have the server side read the POST body directly and handle it, even Content-Type is missing for the sick of IE.
According to http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx
You cannot send content-type : application/json in xdr. You can not even send any other custom header.
xdr is only limited to text/plain
The best you can do is use JSON.stringify() and parse it on server side.
xhr.send(JSON.stringify(jsonObject))

Resources