How can I send a file along the headers of a POST call, while having regular data in the request body - angularjs

I had an existing POST API which was used to sent an object to server. Now I want to attach a file to the header of the same file. Is it possible to do such a thing. Can a request have multiple content-type.
I tried using ngFileUpload directive, and provide my object to data field of Upload.upload method. However, by doing this the entire data object seems to go in header and the request fails.
Any suggestions for sending file in headers and JSON in request body of a POST call??

I eventually found out that a HTTP request can have a single Content-type. This is as per the standards. However it is still possible and quite easy to send data along with a file using the FormData Object. The support for FormData though is not across all the versions of various browsers.

Related

POST files using HTTP action - encoding problems

I have problems to upload attachments to JIRA REST API using HTTP POST with the HTTP action. It works with text files (Content-Type: text/plain) but when I'm posting other files such as PDF and images the files are not uploaded correctly; they get the wrong file size and are not readable.
I assume it's a problem with the encoding. I've tried to use the binary function on the file contents but it does not help. According to JIRA the REST API doesn't accept base64-encoded files.
Have anyone of you been successful in posting attachments (other than text files) to JIRA or another API?
I found how to do it. The body of the HTTP request should contain special attributes for Multipart and Content-Type as described here: https://learn.microsoft.com/en-us/azure/connectors/connectors-native-http#content-with-multipartform-data-type
So the HTTP POST should look like this:

Access-Control-Allow-Origin Issue with API

I have written a pretty simple API in PHP and am running it as a service (https://protoapi-dot-rehash-148415.appspot.com/events/).
When I try to load a data grid with the JSON from the API, I am getting the dreaded "No 'Access-Control-Allow-Origin' header is present on the requested resource." error on the page on which I want to consume the JSON. (http://proto-angular-dot-rehash-148415.appspot.com/events.php)
I've tried a couple of different methods to add Access-Control-Allow-Origin: "*" to the app.yaml file and to the header in the PHP file that produces the API. I think it doesn't work in the yaml because you cannot apply http_headers to dynamic files, and it doesn't work in the file because of the compression.
Is there any other way to make this work, short of putting the API and the app in the same service? I'd hate to do that because I am using mod_rewrite for the API and it will probably cause chaos on my app.
Any insights would be greatly appreciated!
-Mike
The header won't do any good unless you add it server-side, on the events API. The server is what dictates CORS permissions. You could send it messages or files all day with the right headers at the top and it will just ignore them. The allow-origin header has to come from the server to allow the cross-origin resource sharing (CORS) to take place.
I would recommend prepending the header in the function that offers up the API or handles the requests. Your events API spits out a lot of JSON. Right before that JSON, have your API spit out the header Access-Control-Allow-Origin: * and you should be all set.
As a sanity check you can also try adding Access-Control-Allow-Headers: Content-Type and see if that helps. Based on your comment about the Content-Type header, this may be part of the problem. It should be added the same way as the other one; have your API send it prior to your events JSON on its own line (put a \n to make a new line inside the string literal).

duplicate ajax calls in angularjs

I am using express-jwt to build a restful api. Now the client is making duplicate ajax calls, for the first one the initiator is angularjs and for the second one the initiator is other. The first one gets 204 as the response code and the second one gets 200 as the response code. I tried to debug to get to the source of this duplicate requests, but I am not able to.
Below is the header details for the one with 204 status code
Below is the header details for the one with 204 status code
Can any one suggest what could be the issue?
The first call is OPTIONS type. That's a pre-flight call which a browser sends if the page and api are not on same domain.
The purpose of this call is to deal with CORS. Backend usually needs to send the allowed request method types (GET, POST etc.). The browser will then send the real call if the desired request type is among those returned.
Here's a sample of the response headers.
You can ignore it for all intents and purposes. It does not contain any normally useful payload or return data.
Take a look at AJAX in Chrome sending OPTIONS instead of GET/POST/PUT/DELETE? for more info.
Those two requests are different one is OPTIONS and other is GET.
For cross-domain requests, setting the content type to anything other than application/x-www-form-urlencoded, multipart/form-data, or text/plain will trigger the browser to send a preflight OPTIONS request to the server.
You need to handle in the server when the request method OPTIONS , then you need to exit with out processing.

Explain and example about 'get', 'delete', 'post', 'put', 'options', 'patch', 'head' method?

I'm writing a webservice. Could any one explain these above methods and give me some example about them? Thank for your help.
GET should be used to retrieve data with no other effect however you can use query params in url to post data using get but it is not a safe method.
The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line.Generally used to create new entity.
The PUT method requests that the enclosed entity be stored under the supplied Request-URI. Generally used to update existing entity.
The PATCH method applies partial modifications to a resource
The DELETE method requests that the origin server delete the resource identified by the Request-URI.
The TRACE method echoes the received request so that a client can see what (if any) changes or additions have been made by intermediate servers.
The HTTP CONNECT method method starts two-way communications with the requested resource. It can be used to open a tunnel usually to facilitate SSL-encrypted communication (HTTPS) through an unencrypted HTTP proxy.
The OPTIONS method allows the client to determine the options and/or requirements associated with a resource, or the capabilities of a server, without implying a resource action or initiating a resource retrieval.
HEAD Retrieve all resources in a collection (header only) i.e. The HEAD method asks for a response identical to that of a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content.
From this_link you can get a detail about these methods.I have used these resources to write these methods in short.
You can also get simplified details on this wikipidea page.
This stackoverflow link is also very descriptive for http methods.
And for the implementation part this open source Django_rest_code at github can be a very good example to look at how to implement these Http methods in Django(Python).
They are actions from the perspective of the client:
GET refers to the client requesting information in the form of a URL request to the server ie loading a web page full of data.
POST is the client sending information back to the server ie clicking submit on a text field.
PUT is very similar to POST except that the information sent back to the server must be identified under the supplied Request-URI
DELETE requests that the server delete the entity that the client has designated ie removing a blog post from your blog tells the server to forget that information.
Those are the 4 main methods through which clients and servers communicate, thus how information on the server is displayed to and controlled by the client.

CakePHP sending POST data

Inside my CakePHP 2.4 application I need to send POST headers inside my response.
It seems like Cakeresponse can send headers with the response:
http://book.cakephp.org/2.0/en/controllers/request-response.html#cakeresponse
Is there a similar way to put POST data inside response ?
Sending POST data without a form is mentioned here:
Sending POST data without form
and here
POST without Form Method (PHP)
But an AJAX solution is not suitable for me. I solved problem with sending data with headers. I created my own headers, added them to current headers and send data.

Resources