Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specified value has invalid HTTP Header characters

Im trying to access WOWZA Streaming Cloud through their REST API from an ASP.NET site, however have little experience in RESTSharp.

Here is an example curl I'm trying to create:

curl -H 'wsc-api-key: KEY' -H 'wsc-access-key: KEY' -H 'Content-Type:
application/json' -X POST -d '{"live_stream": {"name": "MyNewLiveStream",
"transcoder_type": "transcoded", "billing_mode": "pay_as_you_go",
"broadcast_location": "us_west_california", "encoder": "other_rtmp",
"delivery_method": "push", "aspect_ratio_width": 1920,
"aspect_ratio_height": 1080}}'https://api.cloud.wowza.com/api/v1/live_streams/

see: https://sandbox.cloud.wowza.com/apidocs/v1/

Here is the c# code im using:

var client = new RestClient("https://api.cloud.wowza.com/");
        var newRequest = new RestRequest("api/v1/live_streams",Method.POST);
        newRequest.AddHeader("wsc-api-key", ConfigurationManager.AppSettings["WowzaAPIKey"]);
        newRequest.AddHeader("wsc-access-key", ConfigurationManager.AppSettings["WowzaAccessKey"]);
        newRequest.AddHeader("Content-Type", "application/json");
        var body = "{\"live_stream\": {" +
            "\"aspect_ratio_height\": 720," +
            "\"aspect_ratio_width\": 1280," +
            "\"billing_mode\": \"pay_as_you_go\"," +
            "\"broadcast_location\": \"us_west_california\"," +
            "\"closed_caption_type\": \"none\"," +
            "\"delivery_method\": \"push\"," +
            "\"encoder\": \"wowza_gocoder\"," +
            "\"hosted_page\": true," +
            "\"hosted_page_sharing_icons\": true," +
            "\"name\": \"MyLiveStream\"," +
            "\"player_countdown\": false," +
            "\"player_responsive\": true," +
            "\"player_type\": \"original_html5\"," +
            "\"player_width\": 0," +
            "\"recording\": false," +
            "\"target_delivery_protocol\": \"hls\"," +
            "\"transcoder_type\": \"transcoded\"," +
            "\"use_stream_source\": false}";

        newRequest.AddJsonBody(body);
        IRestResponse myResponse = client.Execute(newRequest);

Following modifications based on the responses, the response code is now

"{\"meta\":{\"status\":401,\"code\":\"ERR-401-InvalidApiKey\",\"title\":\"Invalid Api Key Error\",\"message\":\"Invalid API key.\",\"description\":\"\",\"links\":[]}}"

like image 788
A. Claudi Avatar asked Mar 04 '26 16:03

A. Claudi


2 Answers

Your headers are wrong- you dont need to repeat content-type in value and remove : from the api-key name:

 newRequest.AddParameter("content-type", "application/json"); 
 newRequest.AddParameter("wsc-api-key", ConfigurationManager.AppSettings["WowzaAPIKey"]);

Update as OP updated the question

So now after the modification, the call is through and is working fine. The current error is regarding invalid api key you are passing - may be the access key/api key you are passing is invlaid/expired.

like image 102
Developer Avatar answered Mar 06 '26 07:03

Developer


Thanks everyone for the help - i can now interact seamlessly with Wowza Cloud Streaming through the REST api wrapped by C# and using restsharp. The solution in the end was to use the visual studio function to generate template JSON classes through Paste Special (see In C#, how do I model a JSON object with multiple nested arrays?) - generating the WowzaLivestream class.

Then using this code in the end:

_restClient = new RestClient("https://api-sandbox.cloud.wowza.com");
var newRequest = new RestRequest("api/v1/live_streams", Method.POST);
newRequest.RequestFormat = DataFormat.Json;

        newRequest.AddHeader("wsc-api-key", ConfigurationManager.AppSettings["WowzaAPIKey"]);
        newRequest.AddHeader("wsc-access-key", ConfigurationManager.AppSettings["WowzaAccessKey"]);
        newRequest.AddHeader("Content-Type", "application/json");
        newRequest.AddHeader("Accept", "application/json");

        var requestBody = new WowzaLivestream
        {
            live_stream = new Live_Stream
            {
                aspect_ratio_height = 1024,
                aspect_ratio_width = 1900,
                billing_mode = "pay_as_you_go",
                broadcast_location = "eu_ireland",
                closed_caption_type = "none",
                delivery_method = "push",
                encoder = "other_rtmp",
                name = "WowzaLStest2: " + DateTime.Now.ToShortTimeString(),
                player_countdown = true,
                player_responsive = true,
                player_type = "original_html5",
                player_width = 0,
                recording = false,
                target_delivery_protocol = "hls",
                transcoder_type = "transcoded",
                use_stream_source = false
            }
        };

        var json = newRequest.JsonSerializer.Serialize(requestBody);
        newRequest.AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);
        IRestResponse myResponse = _restClient.Execute(newRequest);

It turns out that the URL used was wrong - it isnt specifically stated in the documentation but for sandbox use, the URL is https://api-sandbox.cloud.wowza.com.

like image 34
A. Claudi Avatar answered Mar 06 '26 06:03

A. Claudi