Superagent: PUT-ing multipart form data - multipartform-data

Is it possible to do a PUT request with multipart form data?
With Superagent I would expect the following to work, but it doesn't.
var request = Request
.put("http://localhost:8080/upload_file")
.field("name", file.name)
.field("size", file.size)
.attach("file", file.file, file.file.name)
.accept("application/json")
If I do a post, it works. The difference is the Content-Type. With the successful post request the Content-Type is multipart/form-data; boundary=------WebKitFormBoundaryXg34NkBFcYWq60mH.
If I were to set this manually how would I know what the boundary should be? It seems to be automatically generated by Superagent.

You should probably do a POST, per Tum's comment.
If I were to set this manually how would I know what the boundary should be? It seems to be automatically generated by Superagent.
You should let Superagent manage that by itself - don't try to set the type yourself, leave off the type call and it will include the correct boundary identifier when it sets it as multi-part itself.

No, it is not possible to do a PUT request with content-type multipart/form-data due to an underlying limitation in PHP as discussed here: https://bugs.php.net/bug.php?id=55815
You might want to take a look at a 'hack' that was done for Symfony in Chekote/symfony: https://github.com/Chekote/symfony/commit/dc1279b2e4c0e9cbcb5b7d578891c31dd878b43b

Related

Error by reading Header values from Logic App HTTP Request

In a if condition I tried to get a header value (secret key) I'am sending to the Logic App.
The following code is used to get the secret from the header (In this example i have parsed it before)
#body('Parse_Header_from_HTTP_Request')?['headers']?['Secret']
I have also tried the solution from this answered question:
Azure Logic Apps : Get HTTP Request Header Key Value into Conditional Check
The variable is null if I check it on the Run History.
According to your description, I do not know how you parse your header before.
Here I use Parse Json to achieve, you could refer to the screenshot as below:
After setting body('Parse_Json')?['name'] to get name value, the output is as below:
As you have said, you have parsed the header after http request. So, after parsing headers, the value is from parse_json, while you refer to the link you provided which value is from http request header.
Hope it helps you.

$http.post URL is getting trimmed off if URL has "#"

I'm having one Rest API: /myApp/fetchData/User-Name/Password. User-Name and Password will be changed based on the request.
When i call the above restapi like this
/myApp/fetchData/srikanth/Abcdef#g123
the request is going like this:
/myApp/fetchData/srikanth/Abcdef
Basically in the URL text got removed from # character. Is there any way to solve?
Thanks,
Srikanth.
In a URI the # triggers the begins of the "fragment", and ends the path. The fragment usually specify a portion of the resource identified by the path.
When you are post-ing the request from the client, you have to escape special characters. Your request should be:
/myApp/fetchData/srikanth/Abcdef%23g123
There are different way to escaping urls, like the encodeURI or encodeURIComponent function in JS. For example, you may do:
var request = "/myApp/fetchData/srikanth/" + encodeURIComponent("Abcdef#g123");
Then the server have to decode back to the original request.
But: are you sure it is a good solution to send the password plain in that way?

AngularJS $http service has CORS issue. But it should be working for JSONP, right? [duplicate]

I'm trying to load an external page using JSONP, but the page is an HTML page, I just want to grab the contents of it using ajax.
EDIT: The reason why I'm doing this is because I want to pass all the user information ex: headers, ip, agent, when loading the page rather than my servers.
Is this doable? Right now, I can get the page, but jsonp attempts to parse the json, returning an error: Uncaught SyntaxError: Unexpected token <
Sample code:
$.post('http://example.com',function(data){
$('.results').html(data);
},'jsonp');
I've set up a jsfiddle for people to test with:
http://jsfiddle.net/8A63A/1/
http://en.wikipedia.org/wiki/JSONP#Script_element_injection
Making a JSONP call (in other words, to employ this usage pattern),
requires a script element. Therefore, for each new JSONP request, the
browser must add (or reuse) a new element—in other words,
inject the element—into the HTML DOM, with the desired value for the
"src" attribute. This element is then evaluated, the src URL is
retrieved, and the response JSON is evaluated.
Now look at your error:
Uncaught SyntaxError: Unexpected token <
< is the first character of any html tag, probably this is the start of <DOCTYPE, in this case, which is, of course, invalid JavaScript.
And NO, you can't use JSONP for fetching html data.
I have done what you want but in my case I have control of the server side code that returns the HTML.
So, what I did was wrapped the HTML code in one of the Json properties of the returned object and used it at client side, something like:
callback({"page": "<html>...</html>"})
The Syntax error you are facing it's because the library you're using expects json but the response is HTML, just that.
I've got three words for you: Same Origin Policy
Unless the remote URL actually supports proper JSONP requests, you won't be able to do what you're trying to. And that's a good thing.
Edit: You could of course try to proxy the request through your server …
If you really just want to employ the client to snag an HTML file, I suggest using flyJSONP - which uses YQL.. or use jankyPOST which uses some sweet techniques:
jankyPOST creates a hidden iframe and stuffs it with a form (iframe[0].contentWindow.document.body.form.name).
Then it uses HTML5 (watch legacy browsers!) webMessaging API to post to the other iframe and sets iframe's form elements' vals to what u specified.
Submits form to remote server...done.
Or you could just use PHP curl, parse it, echo it, so on.
IDK if what exactly ur using it for but I hope this helps.
ALSO...
I'm pretty sure you can JSONP anything that is an output from server code. I did this with ClientLogin by just JSONPing their keyGen page and successfully consoleLogged the text even though it was b/w tags. I had some other errors on that but point is that I scraped that output.
Currently, I'm trying to do what you are so I'll post back if successful.
I don't think this is possible. JSONP requires that the response is rendered properly.
If you want another solution, what about loading the url in an iframe and trying to talk through the iframe. I'm not 100% positive it will work, but it's worth a shot.
First, call the AJAX URL manually and see of the resulting HTML makes sense.
Second, you need to close your DIV in your fiddle example.

Postman: How to pass an array within an http header

So far I tried: (In Bulk Edit)
myHeader:myValue1,myValue2,myValue3
myHeader:[myValue1,myValue2,myValue3]
myHeader:{myValue1,myValue2,myValue3}
Unfortunately I cannot say if one of these should work as I'm using a foreign API and I don't know if I'm maybe doing wrong something different which lets the request fail.
This answer How to pass an array within a http header? seems wrong to me as the answer suggests to put the header in the params...
You can add key value parameters from the postman desktop app. You can add the content-type, auth token and things like that. Here is an example. Good Luck!
Maybe too late, but for others:
Try to add your header multiple times

can not get headers from response in Ext.Ajax.request success callback

I am using Ext.Ajax.request(in sencha toucn 2.3) to post data to my backend server.
And i want to read some custom headers from the "response" parameter of the "success" callback like this:
response.getResponseHeader('x-user-session');
i am quite sure the backend server send back many headers and the 'x-user-session' header also present, but what strange is: when using the "response" object, i can get only one header : "content-type".
this question has already been asked here: ExtJS 4.2.1: Cannot retrieve HTTP Response headers upon Ext.ajax.request callback , but there is no answer yet.
I have already tried debug into the "Ext.data.Connection" class, and found the headers of "response" object in "Ext.data.Connection" also contains only "content-type" header.
could anybody tell me why this happened? is there something wrong in my backend program? or just a bug of Ext.Ajax? how can i get the value of my custom header 'x-user-session'?
thanks ~~
==================ANSWER=====================
i finally got the answer from sencha forum: http://www.sencha.com/forum/showthread.php?289879-the-response-for-Ext.Ajax.request-doesn-t-include-all-response-headers
here is the part about the headers:
Access-Control-Expose-Headers (optional) -
The XMLHttpRequest 2 object has a getResponseHeader() method that returns the value of
a particular response header. During a CORS request, the getResponseHeader() method
can only access simple response headers.
Simple response headers are defined as follows:
Cache-Control
Content-Language
Content-Type
Expires
Last-Modified
Pragma
If you want clients to be able to access other headers, you have to use the
Access-Control-Expose-Headers header. The value of this header is a comma-delimited
list of response headers you want to expose to the client.
i haven't verify it yet, but it seems on the right track :)
enclosed the screen captures:

Resources