How do I access the output of response as string

That sounds as weird as it is. Usually you will access the body by response.body, but in this case all the informations are not there.
So when I do
console.log(response)
I see the informations I wanna grab but if I do
console.log(response.body)
I cannot

but I cannot do response.toString() or String(response) so how do I get the same info into a string as I can see in the console by using console.log(response)?.

Hi @maxi,
what is the expected data format of your data?
Can you post what you’re getting so we can double-check that you’re seeing the expected output, please? Remember to mask the private data if you get them.

Also, you could check the Correlation and Dynamic Data page in the documentation where there are some examples of how to manage the response body with different formats.

I am unsure I can show any of it, and it is allot this site throws at me. But basically I need to see the following:

<input id="ViewId" name="ViewId" type="hidden" value="un58bb745ea7974bdb9026c368cb6bd707" />

which I can see via chrome in the Network tap under Responses

And the result in the response.body in k6 is this long winded text wherer I will give you certain examples out of it.

<link rel="stylesheet" type="text/css" href="/DXR.axd?r=102_235,102_236,1_50,1_53,1_51-0SERj&p=8a3ac8f6" /><link rel="stylesheet" type="text/css" href="/DXR.axd?r=102_239,0_5681-0SERj&p=8a3ac8f6" /><link rel="stylesheet" type="text/css" href="/DXR.axd?r=0_5682,1_40,1_41,1_17,102_108,102_109,1_16,102_112-0SERj&p=8a3ac8f6" /><link rel="stylesheet" type="text/css" href="/DXR.axd?r=102_120,102_121,102_124,1_18,1_19,102_137,102_138,4_13,4_1,4_2,102_141,5_1,102_185,4_7,4_8-0SERj&p=8a3ac8f6" /><link rel="stylesheet" type="text/css" href="/DXR.axd?r=1_28,1_29-PJ0mi&p=8a3ac8f6" /><link rel="stylesheet" type="text/css" href="/DXR.axd?r=1_4-PJ0mi&p=8a3ac8f6" /><link rel="stylesheet" type="text/css" href="/DXR.axd?r=0_5657-NJ0mi&p=8a3ac8f6" /><link rel="stylesheet" type="text/css" href="/DXR.axd?r=0_5658-NJ0mi&p=8a3ac8f6" /><link rel="stylesheet" type="text/css" href="/DXR.axd?r=0_5665,0_5666-NJ0mi&p=8a3ac8f6" /><link rel="stylesheet" type="text/css" href="/DXR.axd?r=0_5669,0_5670-NJ0mi&p=8a3ac8f6" /><link rel="stylesheet" type="text/css" href="/DXR.axd?r=0_5673-NJ0mi&p=8a3ac8f6" /><link rel="stylesheet" type="text/css" href="/DXR.axd?r=0_5674-NJ0mi&p=8a3ac8f6" /><link rel="stylesheet" type="text/css" href="/DXR.axd?r=0_5685,0_5686-NJ0mi&p=8a3ac8f6" /><link rel="stylesheet" type="text/css" href="/DXR.axd?r=0_5661,0_5662,0_5677,0_5678,102_189,102_190,16_5-0SERj&p=8a3ac8f6" /><link rel="stylesheet" type="text/css" href="/DXR.axd?r=16_6,16_11,16_7,16_8,102_193-0SERj&p=8a3ac8f6" /><link rel="stylesheet"

[…]

</div><script id="dxss_140432131" type="text/javascript">
<!--
ASPx.createControl(MVCxClientFormLayout,'formLayoutZurKorrekturPopup','',{'accessibilityCompliant':true,'adaptivityMode':'SingleColumnWindowLimit','switchToSingleColumnAtWindowInnerWidth':800},null,null,{'items':[['formItemun6d3590cf917e4b6d9593d26cac44346f','0',,,,,],['formItemunfe11ada9b5af400a8449307e8187be61','1',,,,,],['formItemun4aa918794ba24131978499c65ba70f86','2',,,,,],['formItemun1f008142e75f470e9f7bba4bbc61c5bf','3',,,,,],['formItemun05e3b26b6e5444209f9906866df480e8','4',,,,,],['','5',,,,],['formItemun09ea737442a24d1eb9768617a6e20f65','6',,,,,],['formItemAbrechnungId','7',,false,,,],['formItemViewId','8',,false,,,]],'captionAssociatingItems':[]});

//-->

I hope this helps.

Hello Maxi! Just bouncing some ideas here.

So when I do
console.log(response)
I see the informations I wanna grab but if I do
console.log(response.body)
I cannot

My first thought is that if console.log(response) has the information you need, why not just use that? Or have a I misunderstood?

My second thought is that that response isn’t JSON, so the result of response.body is going to be unpredictable. Can the API return JSON? If so, do your requests have the accept: application/json header?

Does the “long-winded text” contain the information you need somewhere? If so, something like findBetween might help you extract it. The “Correlation and Dynamic data” doc that Ivan linked in the preceding post has an example of using it. If it does not contain the text you need, your strategy probably needs to take one step backwards.

Okay for the first ting, that is what I am asking. Using just response as a string is not working, trying to stringify it with response.toString() or String(response) only results into a string saying [ object, object ]. I really would like to use response.

The json head, I am not so sure abou it. The API is very unknown to me, I probably will understand more of it in an upcomming contract but not for this project. But I can give you the request I am sending.

      response = http.post(
        'http://' + host + '/Kostennachweis/Kostennachweis/Index',
        {
          abrechnungId: id,
          __RequestVerificationToken: token.kostennachweis,
        },
        {
          headers: {
            'content-type': 'application/x-www-form-urlencoded',
            origin: 'http://' + host + '',
            'upgrade-insecure-requests': '1',
            'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
          },
          timeout: '300s'
        }
      )

Hi @maxi,
the body’s response you’re getting is HTML so you should use the native k6’s HTML API. Check Response.html() documentation for knowing how to use it.

Let me know if it helps

1 Like

that doesn’t seem to help me, I tried two things:

  console.log(input.html());
  console.log(input.html().find('ViewId'));

and I’ve got

INFO[0039] {"url":"http://localhost:82/Kostennachweis/Kostennachweis/Index"}  source=console
INFO[0040] {"url":"http://localhost:82/Kostennachweis/Kostennachweis/Index"}  source=console

maybe I am missunderstanding the ussage here.

Can you show us if your calling the same endpoint/url as on web? If yes have you tried feeding in the user-agent header.

Make sure you add this line to your header’s
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"

putting that in doesn’t change anything sadly

      response = http.post(
        'http://' + host + '/Kostennachweis/Kostennachweis/Index',
        {
          abrechnungId: id,
          __RequestVerificationToken: token.kostennachweis,
        },
        {
          headers: {
            "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36",
            'content-type': 'application/x-www-form-urlencoded',
            origin: 'http://' + host + '',
            'upgrade-insecure-requests': '1',
            'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
          },
          timeout: '300s'
        }
      )

vs the browser


Thanks for the follow up! Do you mind showing the response body, too?

I thought I did, you mean the response body in k6? If you want I can show the full body too.

In the second message I put in this thread I showed examples of gthe beginning and one middle part, it doesn’t get much more exciting but if you want I could copy the whole giant thing.

I tried putting the whole response.bod in here but it is tooo big!!!. Please just take the stuff I have posted already.

Hi @maxi,
as the k6 html.find documentation mentions it uses the jQuery syntax. If you want find by ViewId you should use the # symbol.

  input.html().find('#ViewId');

It could help you if you set up a smaller project to replicate the issue, with a static content. We have a similar example in the documentation, check the Element example here.

well thanks for the advice, but I really need a quick fix here if possible. It is the last code in this programm and for some reason this is the only time it doesn’t really work.

Your solutions sadly don’t improofe anything sadly.

I really would like to know where I get what the console.log gets when I just ask to log response allone. Because it displays to me the stuff I need. But I cannot stingify it.

Okay I found a cheaper solution, I simply did it via

JSON.stringify(input)

this works. but I really shoul look into the new mthods you show. That is probably more usefull in the future.