5

I'm using the chrome devTools to mirror a webRequest. Looking at the network request, there is some JSON data in the response that I want to get access to

Right click --> Copy as fetch -->

fetch(
    "https://www.url.com/service.svc?action=FindConversation&ID=-40&AC=1",
    {
        "credentials":"include",
        "headers":{
            "accept":"*/*",
            "accept-language":"en-US,en;q=0.9",
            "action":"FindConversation",
            "content-type":"application/json; charset=UTF-8",
            "actionid":"-40",
            "unique_identifier":"062lCufCY0i5mI9NMTRUsF87XDq9ttYIonzZQjBcCOPvzoIJFOTSI6ZVNK9lMwy_iPFY2tuZzPY."
            "x-requested-with":"XMLHttpRequest"
        },
        "referrer":"https://ballard.amazon.com/OWA/",
        "referrerPolicy":"no-referrer-when-downgrade",
        "body":"contains some body data I want to manipulate",
        "method":"POST",
        "mode":"cors"
    }
).then(res => {console.log(res)})

This prints out something like this:

Response {type: "basic", url: "https://url/service.svc?action=FindConversation&ID=-40&AC=1", redirected: false, status: 200, ok: true, …}
body: ReadableStream
locked: false
__proto__: ReadableStream
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "basic"
url: "https://url/OWA/service.svc?action=FindConversation&ID=-40&AC=1"
__proto__: Response

When I inspect the network request I just made, it looks like it isn't returning any JSON data, but responds with a 200 code. Is that normal?

I either expected it to return JSON data or fail.

Also, where would the JSON response data be in the res?

2
  • 2
    fetch returns a Promise that resolves to a request. Call .json() on the request and it will return a Promise that resolves to an Object. Commented Apr 1, 2019 at 17:34
  • 2
    expanding on what @zero298 mentioned: .then(response => response.json()) .then(data => console.log(JSON.stringify(data))) are what the 2 promises will look like. Commented Apr 1, 2019 at 18:03

1 Answer 1

7

This is normal behavior. fetch() returns a stream object and not just the body.

Use res.json() to extract the JSON content. For non JSON responses, use res.text()

Sign up to request clarification or add additional context in comments.

3 Comments

and if its HTML is in the return?
@MorganAllen OP Wanted JSON response. For HTML or non-JSON bodies, use .text()
@MorganAllen My apologies, I did not notice. Glad I helped 😀

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.