Giter Club home page Giter Club logo

Comments (18)

kriskowal avatar kriskowal commented on August 31, 2024

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.

karlwestin avatar karlwestin commented on August 31, 2024

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.

kriskowal avatar kriskowal commented on August 31, 2024

@boxxxie can you verify and close?

from q-io.

kriskowal avatar kriskowal commented on August 31, 2024

Thanks!

from q-io.

boxxxie avatar boxxxie commented on August 31, 2024

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.

wmertens avatar wmertens commented on August 31, 2024

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.

cerebrl avatar cerebrl commented on August 31, 2024

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.

kriskowal avatar kriskowal commented on August 31, 2024

@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.

cerebrl avatar cerebrl commented on August 31, 2024

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.

kriskowal avatar kriskowal commented on August 31, 2024

@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.

cerebrl avatar cerebrl commented on August 31, 2024

I'll try to write you up a gist tomorrow. It's getting a bit late tonight :) Thanks!

from q-io.

kriskowal avatar kriskowal commented on August 31, 2024

I’ll be here. Thanks!

from q-io.

cerebrl avatar cerebrl commented on August 31, 2024

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.

kriskowal avatar kriskowal commented on August 31, 2024

Thanks, that’s insightful.

from q-io.

OliverJAsh avatar OliverJAsh commented on August 31, 2024

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.

newtriks avatar newtriks commented on August 31, 2024

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:

  1. Adding Content-Length to headers ensures that the JSON is passed in the body.
  2. 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.

doapp-ryanp avatar doapp-ryanp commented on August 31, 2024

@newtriks thanks for the example. FWIW I did not need to set the Content-Length header.

from q-io.

jonahbron avatar jonahbron commented on August 31, 2024

@newtriks Thanks for that example. As soon as I added the content-length, it worked 😦

from q-io.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.