Home > Back-end >  how to insert json data in curl using visual studio code
how to insert json data in curl using visual studio code

Time:01-04

I am trying to insert the data using curl in Visual studio code.

PS C:\Golang> curl -v  http://localhost:9090/  -d '{"id": 1, "name": "tea","description": "Nice Tea"}' |jq
*   Trying 127.0.0.1:9090...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to localhost (127.0.0.1) port 9090 (#0)
> POST / HTTP/1.1
> Host: localhost:9090
> User-Agent: curl/7.83.1
> Accept: */*
> Content-Length: 35
> Content-Type: application/x-www-form-urlencoded
>
} [35 bytes data]
* Mark bundle as not supporting multiuse
< HTTP/1.1 400 Bad Request
< Content-Type: text/plain; charset=utf-8
< X-Content-Type-Options: nosniff
< Date: Tue, 03 Jan 2023 12:31:05 GMT
< Content-Length: 25
<
{ [25 bytes data]
100    60  100    25  100    35    989   1385 --:--:-- --:--:-- --:--:--  2857
* Connection #0 to host localhost left intact
curl: (3) unmatched close brace/bracket in URL position 4:
Tea}
   ^
parse error: Invalid numeric literal at line 1, column 7
PS C:\Golang>

The structure definition for the request is

type Product struct {
    ID          int     `json:"id"`
    Name        string  `json:"name"`
    Description string  `json:"description"`
    Price       float32 `json:"price"`
    SKU         string  `json:"sku"`
    CreatedOn   string  `json:"-"`
    UpdatedOn   string  `json:"-"`
    DeletedOn   string  `json:"-"`
}

I went through some of the older question they were suggesting to send the data as d '{"id": 1", "name": "tea","description": "Nice Tea"}' but still it failed. Please help me and thanks in advance.

CodePudding user response:

Try escaping quotation marks like below:

curl -v --data '{\"id\": 1, \"name\": \"tea\",\"description\": \"Nice Tea\"}'  http://localhost:9090/

Or if you have complex json data and you want to avoid escaping, you can pass in the json request body as a file:

curl -v  -d '@dummy.json'  http://localhost:9090/
  • Related