Visual studio web test for api request failure 

2019-09-05 15:47发布

问题:

I'm trying to hit a Oauth2 secure Web API from a visual studio web test. I'm using a custom WebTestPlugin and custom WebTestRequestPlugin. The web test plugin simply gets the token from azure active directory and shoves into the request. That seems to be working just fine. I can see the outgoing request for the token comes back in fiddler. I use a web test request plugin to generate a json string string for the outgoing POST request to the API. I can't see that request getting fired off. I keep getting the error: Request failed: Specified value has invalid HTTP Header characters. Parameter name: name

custom Request plugin code

public override void PreRequest(object sender, PreRequestEventArgs e)
    {
        var body = new StringHttpBody
        {
            BodyString = EventBuilder.EventBuilder.BuildSingleAuditEvent(_orgId, _tenantId, _hashKey),
            ContentType = "application/json"
        };
        e.Request.Body = body;
    }

I'm using a service request in visual studio webtest with nothing in the String Body variable.

as you can see the String Body has nothing set in its properties.

here is the fiddler trace of the successful oauth request

What am I missing here?

回答1:

this was an issue with formatting of the auth token. the header value was coming through as: "Authorization: Bearer", "TokenValue" when it should have been "Authorization:", "Bearer TokenValue"