Giter Club home page Giter Club logo

Comments (9)

nathanielyoon avatar nathanielyoon commented on August 30, 2024

I also tried:

new Deno.Command("curl", {
  args: [
    "https://slumbot.com/api/new_hand",
    "--header",
    "'Content-Type: application/json'",
    "-d",
    "'{}'",
  ],
}).output().then(($) => console.log(new TextDecoder().decode($.stdout)));

from slumbot2019.

ericgjackson avatar ericgjackson commented on August 30, 2024

The header should be "Content-Type", not "content-type".

In your second example (with "curl"), I'm not sure why you have the single quotes (e.g., before Content-Type). That could throw things off if they are not stripped somehow.

from slumbot2019.

nathanielyoon avatar nathanielyoon commented on August 30, 2024

I assumed headers are case-insensitive, as per rfc7230, but when changing their case, the Typescript fetch doesn't work.

fetch("https://slumbot.com/api/new_hand", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: "{}",
}).then(console.log);

For the second (Deno.Command), removing the single quotes from the header and body worked, i.e. the program's call to curl returned a successful response. This is why I suspect something in the way Javascript's fetch API makes requests is to blame. I noticed in the response object, the headers are converted to lowercase:

Response {
  body: ReadableStream { locked: false },
  bodyUsed: false,
  headers: Headers {
    connection: "keep-alive",
    "content-length": "0",
    "content-type": "application/json",
    date: "Tue, 18 Jun 2024 12:32:41 GMT",
    server: "nginx/1.18.0 (Ubuntu)"
  },
  ok: false,
  redirected: false,
  status: 400,
  statusText: "Bad Request",
  url: "https://slumbot.com/api/new_hand"
}

There's no option to change this behavior, so if the server treats (required) headers case-sensitively, it may be impossible to make requests from Javascript.

from slumbot2019.

ericgjackson avatar ericgjackson commented on August 30, 2024

I think you're right. I'll change the server code to be case-insensitive in handling HTTP headers and we'll see if that fixes things. Should be able to do this today or tomorrow.

from slumbot2019.

ericgjackson avatar ericgjackson commented on August 30, 2024

I've pushed the fix to be case-insensitive in parsing HTTP headers. Can you try again to see if you can access the API using fetch() from Javascript?

from slumbot2019.

nathanielyoon avatar nathanielyoon commented on August 30, 2024

Running the code from the original post yielded the same (400) response.

Is requiring the content-type header necessary? Presumably, non-JSON bodies would fail regardless.

from slumbot2019.

nathanielyoon avatar nathanielyoon commented on August 30, 2024

I think the header parsing is still case-sensitive (as opposed to another unique characteristic of fetch), cause this failed:

curl -s https://slumbot.com/api/new_hand --header "content-type: application/json" -d "{}"

and this succeeded:

curl -s https://slumbot.com/api/new_hand --header "Content-Type: application/json" -d "{}"

from slumbot2019.

ericgjackson avatar ericgjackson commented on August 30, 2024

Oh, shoot, I hadn't pushed the libraries containing the fix.

Now I think I've pushed the fix and the curl command you provided (with lower-case content-type) is now working. Please try again.

from slumbot2019.

nathanielyoon avatar nathanielyoon commented on August 30, 2024

Yep, works now. Thanks for the fix.

from slumbot2019.

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.