Comments (18)
I’ll write up a test case and see what the problem is. The array is the preferred output, but I might adjust normalizeRequest and normalizeResponse to accept a string.
from q-io.
Hi, i've been looking at a similar thing, you'll need to use a "Content-Type": "application-/x-www-form-urlencoded"
header.
this worked:
// server.js
var connect = require("connect");
var server = connect(connect.bodyParser());
function file(req, res, next) {
if(req.method == "POST") {
console.log("req", req.body, req);
res.writeHead(200);
res.end("hi");
}
}
server.use(file);
module.exports = server;
server.listen(3000);
//------------------------------
// client.js
var fs = require("q-io/fs");
var http = require("q-io/http");
fs.read("./ha.txt").then(function(text) {
var request = {
host: "localhost",
port: 3000,
method: "POST",
body: [text],
headers: { "Content-Type": "application/x-www-form-urlencoded" }
};
return http.request(request);
}).then(function(resp) {
return console.log("uploaded");
}).fail(function(err) {
console.log("failed", err);
});
from q-io.
@boxxxie can you verify and close?
from q-io.
Thanks!
from q-io.
I'm not on nodejs anymore so I can't easily confirm. However if your code works then that would be really helpful for people like me building multi system applications.
from q-io.
I had to do something like (coffeescript)
request =
url: url,
method: 'POST'
body: [("#{key}=#{encodeURIComponent value}" for key, value of formData).join '&']
headers: "Content-Type": "application/x-www-form-urlencoded"
return HTTP.read(request).then( (body) -> JSON.parse body )
It would be nice if q-io could include some sugar to make form passing easy... A project like https://github.com/mikeal/request is of course a lot more complete but how about something like adding a form
key to a request that will convert into the body and headers as above? That's only a few lines of code... Likewise a readJson would be nice?
from q-io.
From what I can tell, this is still very much an issue. It took me hours to realize that any truthy value in the body of the request fails, except for an array. Unfortunately, the receiving end of the request has no body, so the array value is not being passed through. This should be documented somewhere, please! I can't count how many hours I've wasted trying to debug this :(
from q-io.
@cerebrl Can you confirm that the problem was that you passed a string for a request body instead of an array? I will consider fixing that. I definitely intend to produce better documentation. The stuff I have is far too rough and it looks like the users are coming.
from q-io.
Hey Kris! Thanks for the quick reply. The request does go through if I pass an array into the body. But, on the receiving end of the request, req.body
is undefined
. I don't know what happens to the array, but it never shows up on the other side [shrugs]
from q-io.
@cerebrl That is curious. Can you send me a gist or more details about the scenario. Are your client and server both Node.js processes?
from q-io.
I'll try to write you up a gist tomorrow. It's getting a bit late tonight :) Thanks!
from q-io.
I’ll be here. Thanks!
from q-io.
Well, I couldn't help myself: https://gist.github.com/cerebrl/7314665
Let me know if I'm doing something wrong here or if you have any questions.
from q-io.
Thanks, that’s insightful.
from q-io.
The reason it does not error if the body is not an instance of Array
is because there was a missing end to a promise chain.
from q-io.
I was having similar odd issues when posting JSON data to my Rails based api. I did manage to get it working using the above suggestion "Content-Type": "application-/x-www-form-urlencoded"
, however, I really wanted to persist and fathom out why the JSON body appeared to be empty.
The solution I found after trial and error, revealed two interesting results which may or maybe not obvious to others:
- Adding
Content-Length
to headers ensures that the JSON is passed in the body. - Posting an empty body results in a
socket hang up
error.
Here is the code I used which successfully makes a POST request with JSON data within the request body:
var body = JSON.stringify({
token: 'foo',
files: files // Array of filenames
});
var headers = {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body, 'utf8')
};
var request = {
url: apiURL,
charset: 'UTF-8',
method: 'POST',
headers: headers,
body: [body]
};
return http.request(request)
.then(function (response) {
var ok = response.status >= 200 && response.status < 400;
if (!ok) {
throw new Error('API responded with a status code: ' + response.status);
}
});
from q-io.
@newtriks thanks for the example. FWIW I did not need to set the Content-Length
header.
from q-io.
@newtriks Thanks for that example. As soon as I added the content-length, it worked 😦
from q-io.
Related Issues (20)
- Reader().map() fails to limit properly the number of parallel instances of a function. HOT 3
- proxy requests HOT 2
- join() doesn't support network shares HOT 4
- not able to do x-www-unencoded body HOT 5
- Crash on reading large objects HOT 1
- problems by copying / moving data-files with special characters within path-variable HOT 7
- HTTP.read resolves with buffer instead of string HOT 1
- q-io downloading or writing too much data in version 1.13 HOT 14
- Mock: fs.copy() is buggy HOT 1
- removeTree() thinks it fails with ENOTEMPTY, but works on second try HOT 2
- `write()` resolves before content is written HOT 5
- Bump version of collections to avoid conflict with ES6 HOT 3
- filetree compatible with jstree
- `fs.copyTree` does not copy a large amount of files HOT 1
- is v2 up to date? HOT 3
- [v2] Last written file is always empty HOT 1
- Drop usage of collections, it breaks globals HOT 15
- Fixing a Prototype Override Protection Bypass Vulnerability in qs HOT 3
- Connect Timeout setting HOT 1
- Need 'qs' version updated to latest version to resolve Security Vulnerabilities HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from q-io.