从 alexa 语音服务获取 500 错误

Getting a 500 error from alexa voice services

我有一个正在录制语音命令的 AVAudioRecorder,然后我使用以下代码将其转换为 nsdata -

NSData *data = [NSData dataWithContentsOfURL:recorder.url];

现在我使用以下代码执行语音识别器请求 -

NSDictionary *headers = @{ @"Authorization": [NSString stringWithFormat:@"Bearer %@", authCode], @"Transfer-Encoding": @"chunked", @"Content-Type": @"multipart/form-data; boundary=someboundary" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"\r\n--someboundary\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Disposition: form-data; name=\"request\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Type: application/json; charset=UTF-8\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"{\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"messageHeader\": {\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"deviceContext\": [\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"{\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"name\": \"playbackState\",\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"namespace\": \"AudioPlayer\",\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"payload\": {\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"streamId\": \"\",\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; // what is the streamid supposed to be?
[postData appendData:[@"\"offsetInMilliseconds\": \"\",\r\n" dataUsingEncoding:NSUTF8StringEncoding]];//Is this correct?
[postData appendData:[@"\"playerActivity\": \"IDLE\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"}\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"}\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"]\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"},\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"messageBody\": {\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"profile\": \"alexa-close-talk\",\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"locale\": \"en-us\",\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"format\": \"audio/L16; rate=16000; channels=1\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"}\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"}\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"--someboundary\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Disposition: form-data; name=\"audio\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Type: audio/L16; rate=16000; channels=1\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[NSData dataWithData:audioData]];
[postData appendData:[[NSString stringWithFormat:@"\r\n--someboundry--"] dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://access-alexa-na.amazon.com/v1/avs/speechrecognizer/recognize"]
                                                                                                        cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                                                                        timeoutInterval:20.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
            if (error) {
                    NSLog(@"%@", error);
            } else {
                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                    NSLog(@"SpeechRecognition %@", httpResponse);
            }
}];

[dataTask 恢复];

但是上面的代码给我一个 500 错误。非常感谢任何帮助

我知道出了什么问题。音频数据前的内容类型应为

[postData appendData:[@"Content-Type: audio/L16; rate=16000; channels=1\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

不是

[postData appendData:[@"Content-Type: audio/L16; rate=16000; channels=1\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

缺少一个额外的“\r\n”