Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request('get'...) can't pass json data #115

Closed
blakemcbride opened this issue Mar 26, 2015 · 8 comments
Closed

request('get'...) can't pass json data #115

blakemcbride opened this issue Mar 26, 2015 · 8 comments

Comments

@blakemcbride
Copy link

Network traffic see with curl:

GET /components/_search HTTP/1.1
User-Agent: curl/7.35.0
Host: localhost:9200
Accept: */*
Content-Length: 386
Content-Type: application/x-www-form-urlencoded
{
    "from": 0,
    "size": 20,
    "query": {
            "bool": {
                "must": [
                    {"match_phrase_prefix" : {
                        "classifications" : "passive_components/resistors"
                    }},
                    {"match" : {
                        "_all" : "1000"
                    }}
                ]
            }
        }
    }

With needle:

JS Code:

    var options = {
        json: true
    };
    var host = 'http://' + esHost + '/' + path;
    needle.request('get', host, json, options, callback);

Resulting network traffic:

GET /components/_search?from=0&size=20&query[bool][must][0][match_phrase_prefix][classifications]=passive_components%2Fresistors&query[bool][must][1][match][_all]=1000 HTTP/1.1
Accept: */*
Connection: close
User-Agent: Needle/0.9.0 (Node.js v0.12.0; linux x64)
Host: localhost:9200

Thanks.

Blake

@blakemcbride blakemcbride changed the title request('get...) can't pass json data request('get'...) can't pass json data Mar 26, 2015
@tomas
Copy link
Owner

tomas commented Mar 26, 2015

Looks like you're just passing "true" as the options. Try with:


var options = {
  json: true
};
var host = 'http://' + esHost + '/' + path;
needle.get(host, options, callback);

@blakemcbride
Copy link
Author

In your example, where is my json data?

@tomas
Copy link
Owner

tomas commented Mar 26, 2015

Oh, now I see. When passing data to a GET request, Needle builds a query string and appends it to the URL, given that those requests should not contain a body.

@blakemcbride
Copy link
Author

Yea. Whatever you are doing isn't working for me. I am trying to pass json data through a get. Just FYI, CURL works, and the 'request' npm package works too.

@tomas
Copy link
Owner

tomas commented Mar 26, 2015

Why would you want to send a body as part of a GET request anyway? You might want to suggest the developer of that endpoint to stick to the spec. Needle doesn't send bodies on GET requests because there's a very high chance that the server on the other side won't even mind to read it.

@blakemcbride
Copy link
Author

floatdrop added a commit to sindresorhus/got that referenced this issue Apr 3, 2015
@monolithed
Copy link

+1, It will be very useful for many API and more elegant than:

import needle from 'needle';
import url from 'url';

let href = url.parse('https://google.com');

href.query = {
    foo: 1,
};

href = url.format(href);

needle.get(href, (error, response) => {
   // ... 
});

kanayo added a commit to kanayo/needle that referenced this issue Oct 1, 2015
Smarter way of handling request bodies. Should fix tomas#107 and tomas#115
@tomas
Copy link
Owner

tomas commented Jun 7, 2016

Closing this issue, as this was implemented long ago.

screenshot 2016-06-06 at 10 33 15 pm

@tomas tomas closed this as completed Jun 7, 2016
renawolford6 pushed a commit to renawolford6/React-Benchmark-got-repos that referenced this issue Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants