# gRPC Before Invoke doesn't work like REST Pre-request script

**URL:** <https://community.postman.com/t/grpc-before-invoke-doesnt-work-like-rest-pre-request-script/44814>\
**Category:** Help Hub\
**Created:** [March 6, 2023, 3:48pm UTC](https://community.postman.com/t/grpc-before-invoke-doesnt-work-like-rest-pre-request-script/44814 "2023-03-06T15:48:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shabib](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@shabib](https://community.postman.com/u/shabib)\
**Post date:** [March 6, 2023, 3:48pm UTC](https://community.postman.com/t/grpc-before-invoke-doesnt-work-like-rest-pre-request-script/44814/1 "2023-03-06T15:48:13Z")

</div>

Hi,

for some reason, I am able to run a Pre-request script and set up a Collection Variable (for setting up a token) for REST requests, but when I have the same script run as a `Before Invoke` script in a gRPC request it fails with the error message:

> code: “ECONNRESET”  
> name: “Error”  
> message: “socket hang up”  
> stack: “Error: socket hang up  
> at connResetException (node:internal/errors:691:14)  
> at TLSSocket.socketOnEnd (node:\_http\_client:471:23)  
> at TLSSocket.emit (node:events:402:35)  
> at TLSSocket.emit (node:domain:475:12)  
> at endReadableNT (node:internal/streams/readable:1343:12)  
> at processTicksAndRejections (node:internal/process/task\_queues:83:21)”

Do you have any idea why? what am I doing wrong?

Thank you,

Bar.

---

<div class="post-metadata">

**Author:** ![udit](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/udit/32/5056_2.png) [@udit](https://community.postman.com/u/udit)\
**Post date:** [March 6, 2023, 3:50pm UTC](https://community.postman.com/t/grpc-before-invoke-doesnt-work-like-rest-pre-request-script/44814/2 "2023-03-06T15:50:24Z")

</div>

@shabib can you share the script that we can use to reproduce this issue locally?

---

<div class="post-metadata">

**Author:** ![shabib](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@shabib](https://community.postman.com/u/shabib)\
**Post date:** [March 6, 2023, 5:18pm UTC](https://community.postman.com/t/grpc-before-invoke-doesnt-work-like-rest-pre-request-script/44814/3 "2023-03-06T17:18:43Z")

</div>

@udit - there you go

> const options = {  
> url: “https://[[SOME\_URL]]/token”,  
> method: ‘POST’,  
> header: { ‘content-type’: ‘application/json’ },  
> body: {  
> mode: ‘raw’,  
> raw: JSON.stringify([[BODY\_PAYLOAD]])  
> }  
> };
> 
> pm.sendRequest(options, function (err, response) {  
> if (err) {  
> console.error(err);  
> return;  
> }
> 
> ```
> const data = response.json();
> console.info(data);
> pm.collectionVariables.set("jwt", data.token);
> 
> ```
> 
> });

---

<div class="post-metadata">

**Author:** ![udit](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/udit/32/5056_2.png) [@udit](https://community.postman.com/u/udit)\
**Post date:** [March 7, 2023, 4:17am UTC](https://community.postman.com/t/grpc-before-invoke-doesnt-work-like-rest-pre-request-script/44814/4 "2023-03-07T04:17:14Z")

</div>

The following code snippet works fine for me.

```js
const options = {
    url: "https://postman-echo.com/post",
    method: "POST",
    header: { "content-type": "application/json" },
    body: {
        mode: "raw",
        raw: JSON.stringify({ token: "foobar" })
    }
};

pm.sendRequest(options, function (err, response) {
    if (err) {
        console.error(err);
        return;
    }

    const data = response.json();

    console.info(data);

    pm.collectionVariables.set("jwt", data.token);

});

```

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/1/411cd35b5630183ae0ee3e71d1fb0905548038f3.png)

---

<div class="post-metadata">

**Author:** ![shabib](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@shabib](https://community.postman.com/u/shabib)\
**Post date:** [March 7, 2023, 12:29pm UTC](https://community.postman.com/t/grpc-before-invoke-doesnt-work-like-rest-pre-request-script/44814/5 "2023-03-07T12:29:46Z")

</div>

I wonder if it might be because there is a bug in picking up the local certificate I set in the Certificate Setting for the URL I use. can you help me check for that?
