curlconverter / curlconverter

Transpile curl commands into Python, JavaScript and 27 other languages
https://curlconverter.com
MIT License
7.17k stars 867 forks source link

npm module cannot convert to json correctly #622

Closed pp90opk closed 4 months ago

pp90opk commented 4 months ago

with curl command:

curl   -H "Content-Type: application/json; charset=UTF-8" --data-binary "{\"sd\":\"MYVNhQFgtEQ8kwIMXEs=\"}" --compressed "https://event.tatistics"

the webpage converts it correctly, but with nodejs, it parses data wrongly: result with nodejs

{
  url: 'https://event.tatistics',
  raw_url: 'https://event.tatistics',
  method: 'post',
  headers: { 'Content-Type': 'application/json; charset=UTF-8' },
  data: '{sd:MYVNhQFgtEQ8kwIMXEs=}',
  compressed: true
}

result with webpage:

{
    "url": "https://event.tatistics",
    "raw_url": "https://event.tatistics",
    "method": "post",
    "headers": {
        "Content-Type": "application/json; charset=UTF-8"
    },
    "data": {
        "sd": "MYVNhQFgtEQ8kwIMXEs="
    },
    "compressed": true
}

and my nodejs code:

let req = JSON.parse(curlconverter.toJsonString(curl))
        console.log(req)
verhovsky commented 4 months ago

The issue is probably how you're escaping the string. Before this line

let req = JSON.parse(curlconverter.toJsonString(curl))

can you show me the line that's defining the curl variable you're passing to toJsonString()? You're probably not escaping the \ in the bash code to \\ when you're turning it into a JavaScript string. Your code should look like this:

import * as curlconverter from 'curlconverter';

const curl = 'curl   -H "Content-Type: application/json; charset=UTF-8" --data-binary "{\\"sd\\":\\"MYVNhQFgtEQ8kwIMXEs=\\"}" --compressed "https://event.tatistics"'
let req = JSON.parse(curlconverter.toJsonString(curl))
console.log(req)

which produces this result:

{
  url: 'https://event.tatistics',
  raw_url: 'https://event.tatistics',
  method: 'post',
  headers: { 'Content-Type': 'application/json; charset=UTF-8' },
  data: { sd: 'MYVNhQFgtEQ8kwIMXEs=' },
  compressed: true
}

If you're saying the issue is that the keys don't have " around them (as in it's url: instead of "url":), then that's because you're doing JSON.parse(). In that case you need to convert it back into JSON with JSON.stringify():

import * as curlconverter from 'curlconverter';

const curl = 'curl   -H "Content-Type: application/json; charset=UTF-8" --data-binary "{\\"sd\\":\\"MYVNhQFgtEQ8kwIMXEs=\\"}" --compressed "https://event.tatistics"'
let req = JSON.parse(curlconverter.toJsonString(curl))
console.log(JSON.stringify(req, null, "    "))
pp90opk commented 4 months ago

the curl command is copied directly from proxy tool charles, from its Copy Curl Request function, and converted directly without changing anything. in this case, the data would not be parsed correctly as a valid json object.

verhovsky commented 4 months ago

When you convert a bash command into a JavaScript string, you need to respect JavaScript syntax. You can't just paste bash code (that you get from Copy Curl Request) into JavaScript and expect it to work because JavaScript isn't smart enough to parse bash syntax, it expects all strings to be in its own format.

You really just need to search-and-replace "\" with "\\" in your string.