Could anyone please provide advice on how to resolve the issue I encountered when trying to make Post and get Http method calls using protractor?

I am currently facing an issue while trying to make a GET API request using protractor. The challenge lies in using the bearer token generated from a previous POST response in the headers of the GET request. Although I have successfully executed the POST request, I am struggling to incorporate the generated token in the GET request headers. Below is the snippet of code that I have attempted. I would appreciate it if someone could guide me on the correct syntax for this approach.

Note: I have masked the URL and credentials for confidentiality reasons.

var Request = require("request");

describe('post user request', () => {

it('create user test', (done) => {
              
        //1. create user (POST)
        Request.post({
            //  method: 'POST',
            "url": "http://example.com",
            "body" : {
                "username": "abc",
                "password": "abc1",
            }

}).then((res)=>{ console.log(JSON.stringify(res))

        }).then((res) =>{
               const token1 = res.token 
                //2. get user (GET)
                Request.get({
                    // method: 'GET',
                    "url": "http://example.com/xyz",
                    "headers": {
                        "Authorization" : "Bearer " + token1
                    }
                }).then((res)=>{
                    console.log(res)
                    done();
                   
                })
        })
     
   

}) })

Error message : 
F
  post user request
    × create user test
      - Failed: Argument error, options.body.


Failures:
1) post user request create user test
  Message:
    Failed: Argument error, options.body.
  Stack:
    Error: Argument error, options.body.
        at setContentLength (D:\Protractor\node_modules\request\request.js:437:28)
        at Request.init (D:\Protractor\node_modules\request\request.js:442:5)
        at new Request (D:\Protractor\node_modules\request\request.js:127:8)
        at request (D:\Protractor\node_modules\request\index.js:53:10)
        at Function.post (D:\Protractor\node_modules\request\index.js:61:12)
        at UserContext.<anonymous> (D:\Protractor\Specs_Map\APIfile.spec.js:8:21)
        at D:\Protractor\node_modules\jasminewd2\index.j







  

Answer №1

    }).then((response)=>{ console.log(JSON.stringify(response))
this is the point where the issue originates.

At this step, you are logging the result without returning it, which means the subsequent then will not receive any data.

Consider implementing the following approach:

    }).then((response)=>{ console.log(JSON.stringify(response)); return response;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve an object using a variable

Essentially, my question is how to extract a value from a variable and input it into a sequence. Being Dutch, I struggle to articulate this query correctly. var channelname = msg.channel.name; "description": `${config.ticketlist.channelname.ticketmessage} ...

What is the best way to automatically add a date and timestamp to every new entry?

I am currently working on a project that utilizes AngularJS and Ionic frameworks. The main feature of the project will involve users inputting data to create a list, and allowing any user to comment on each item in the list. One challenge I have encounter ...

Guide to create a sliding menu transition from the viewport to the header

I am just starting to learn jQuery and I want to create a menu similar to the one on this website Specifically, I would like the menu to slide down from the viewport to the header, as shown in the template in the link. I have searched through the jQuery ...

In order to locate a matching element within an array in a JSON file and update it, you can use Node

Good day, I have a script that updates the value in a JSON file const fsp = require('fs').promises; async function modifyNumberInFile() { try { let data = await fsp.readFile('example.json'); let obj = JSON.parse(dat ...

What is the process for converting this code to HTML format?

I am new to programming and I am using an API with node.js to display the result in a browser. The API is working fine with console.log, but I want to render it on the browser instead. I am using Jade template for this purpose. How can I write the code t ...

retrieve data properties from an object

Suppose I have the following setup: var myObj = [ { name: 'A', number: 'b1', level: 0 }, { name: 'B', number: 'b2', level: 0 }, ]; I need to figure out how to retrieve all the names and format them like this: ...

The Camera component in React Native is not currently supporting foreground service

Trying to capture images in a foreground service, such as taking pictures while the user is using another app. Everything works fine within the app, but when the app is closed with the foreground service active, the camera stops working and shows this erro ...

Issues encountered when trying to use default color classes in Tailwind CSS

I'm currently working on a React project that utilizes the Tailwind CSS Framework. To integrate Tailwind into my React app, I used NPM to install it in the following way: npm install -D tailwindcss postcss autoprefixer npx tailwindcss init -p After i ...

Angularjs directives failing to compute accurately~

I'm working with an AngularJS directive where the HTML template is compiled within the linker function. var htmlTemplate = '<img ng-src="~/img/logos/{{account.Logo}}" width="45" height="35" />' var linker = function (scope, element) ...

What is the proper method for triggering an animation using an IF statement with A-Frame animation mixer?

I am currently exploring the capabilities of the animation mixer in A-Frame and trying to trigger a specific action when a particular animation is playing (like Animation 'B' out of A, B, C). Although I'm not well-versed in Javascript, I ha ...

The React component continuously refreshes whenever the screen is resized or a different tab is opened

I've encountered a bizarre issue on my portfolio site where a diagonal circle is generated every few seconds. The problem arises when I minimize the window or switch tabs, and upon returning, multiple circles populate the screen simultaneously. This b ...

"Having trouble subscribing? The first attempt doesn't seem to be working

I'm currently working on some TypeScript code that searches for the nearest point around a user in need of car assistance by checking a database. Once the nearest point is identified, the code retrieves the phone number associated with it and initiate ...

Ensure your TypeScript class includes functionality to throw an error if the constructor parameter is passed as undefined

My class has multiple parameters, and a simplified version is displayed below: class data { ID: string; desp: string; constructor(con_ID:string,con_desp:string){ this.ID = con_ID; this.desp = con_desp; } } When I retrieve ...

The feature in AngularJs where ng-options (key, value) is used with ng-model="selected" is not functioning properly

Here is the HTML code I am working with: <select ng-options="value.name for (key, value) in countries track by key" ng-model="selected" > </select> This is the specific object I am trying to manipulate: $scope.countries = { "AFG":{"name" ...

Preventing an image from being repeated when using Canvas drawImage() without having to clear the entire canvas

How can I prevent multiple instances of the same image from smearing across the canvas when drawing it? The platforms seem to stick together and not separate properly. Why do I have to clear the entire rectangle for everything to disappear? Does anyone ha ...

What is the best way to link buttons to specific drop down sections?

How can I create multiple drop down divs with different content for each button click? JSFiddle: http://jsfiddle.net/maddiwu/xe6xtfqh/ .slide { overflow-y: hidden; max-width: 100%; overflow-x: hidden; max-height: 100vw; /* approximate ...

Quickest method for skimming through an extremely lengthy document beginning at any specified line X

In my current project, there is a text file that is written to by a python program and read by another program to display on a web browser. JavaScript handles the reading process at the moment, but I am considering moving this functionality to python. The ...

"Upon choosing a file using the HTML input file multiple element, a triggered event

After selecting multiple files using the input type="file" HTML element, I am eager to start uploading them. Can you tell me which event I should use to execute code immediately after finalizing my file selection? This is what my HTML code looks like: &l ...

Utilizing Typescript for parsing large JSON files

I have encountered an issue while trying to parse/process a large 25 MB JSON file using Typescript. It seems that the code I have written is taking too long (and sometimes even timing out). I am not sure why this is happening or if there is a more efficien ...

Center a grid of cards on the page while keeping them aligned to the left

I have a grid of cards that I need to align in the center of the page and left within the grid, adjusting responsively to different screen sizes. For example, if there are 10 cards and only 4 can fit on a row due to screen size constraints, the first two ...