Getting Error "Could not set middleware Invalid middleware: exit status 1"

30 views
Skip to first unread message

Subhabrata Pal

unread,
Feb 2, 2018, 11:09:24 AM2/2/18
to hoverfly

Hi,  

This is regarding the use of Python script as middleware

using cURL - the script is working as suggested by "Tommy Situ"(thanks for that)

I added  "Content Length " to make it work thru browser


def main():
    payload = sys.stdin.readlines()[0]

    payload_dict = json.loads(payload)
    payload_dict['response']['status'] = random.choice([200, 201])

    if "response" in payload_dict and "body" in payload_dict["response"]:
        payload_dict["response"]["body"] = "'This is a Mock Response'"
payload_dict["response"]["headers"]["Content-Length"] = [str(len(json.dumps(payload_dict))]

    print(json.dumps(payload_dict))

if __name__ == "__main__":
    main()
Getting Error

Could not set middleware

Invalid middleware: exit status 1

Is this regarding invalid content Length?
Or
Error in script?


thanks for any help

Regards
Subho

john.davenport

unread,
Feb 2, 2018, 1:56:16 PM2/2/18
to hoverfly
Hi Subho, it's the script. 

The script uses spaces for indentation but you have inserted that extra line that sets the content length with tabs.

Python has probably interpreted as an extra level of indentation and then complained.

I suggest making sure you always use spaces or at least convert your tabs to spaces. You might also want to use a python mode in your editor. 


We apologise as the "Could not set middleware" message is not the most helpful. We have it on the list of things to change.

Hopefully that will fix the problem.

Subhabrata Pal

unread,
Feb 16, 2018, 10:17:50 AM2/16/18
to john.davenport, hoverfly
Hi John,

Thanks for the response (Sorry for the delay)

I have implemented that part but moment I add the below line - the same error message appears:

payload_dict["response"]["headers"]["Content-Length"] = [str(len(json.dumps(payload_dict))]

Actual the intention is to run the "http://time.jsontest.com/" thru browser and the middle layer should give the response
So I added the content length.



Is there any other way to do this

Regard

--
You received this message because you are subscribed to the Google Groups "hoverfly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hoverfly+u...@specto.io.
To post to this group, send email to hove...@specto.io.
To view this discussion on the web visit https://groups.google.com/a/specto.io/d/msgid/hoverfly/704ddf86-7a50-434a-b62b-ea7c7570b52d%40specto.io.

Reply all
Reply to author
Forward
0 new messages