Direct Line API Request fails with Postman (Status: 502 Bad Gateway)
Version
SDK v4
Describe the bug
I am trying to send an activity after successfully starting a conversation with my bot on Azure over the direct line channel but if i send the request i get a Bad Gateway response inside Postman:

Getting a the conversation ID works fine and sending/reciving a message over the webchat (which uses the Direct Line Channel) also works without problems:

When sending the request i also get this notification:

But what does this mean and how can i fix this issue? How could this problem occur?
To Reproduce
Steps to reproduce the behavior:
- Set an Azure bot up (SDK v4)
- Set up the direct line channel
- Start a new conversation with an API request in Postman (use the URL etc. from the screenshots above with your credentials)
- Send an Activity with an API request in Postman (use the URL etc. from the screenshots above with your credentials)
- Check the error output (the API response)
Expected behavior
The API Request should work successfully.
Screenshots
Screenshots are in my description
Additional context
-
Tracking Status
Dotnet SDK TODO
- [ ] PR
- [ ] Merged
Javascript SDK TODO
- [ ] PR
- [ ] Merged
Python SDK TODO
- [ ] PR
- [ ] Merged
Java SDK TODO
- [ ] PR
- [ ] Merged
Samples TODO
- [ ] PR
- [ ] Merged
Docs TODO
- [ ] PR
- [ ] Merged
Tools TODO
- [ ] PR
- [ ] Merged
Hi @danieldangler,
The 502 error generally happens when your bot cannot be reached, and there are multiple reasons that might be happening. Let's run through some of the basic debugging steps first.
- Where is your bot deployed? Is it on Azure or are you running locally?
- Is the bot running?
- If running locally, are you using ngrok or other tunneling solution? Have you updated the messaging endpoint in Azure?
- Have you double checked your keys?
Have you been following the steps in the Direct Line docs to connect to the bot?
You might also consider running the bot locally and checking to see if any errors pop up while you send Direct Line requests to it.
Hey @anishprasad01 and thanks for your help! My bot lives in Azure and actually works there. However, I still get this error message in the portal when testing in Azure (Webchat) or on my testing page (Direct Line), but still get my desired responses.

When I test the bot locally, I get a very strange response (which is also the error code 500 just like from one of my screenshots above)

{
"value": "System.ArgumentNullException: Value cannot be null. (Parameter 'textReplyToSend')\r\n at Microsoft.Bot.Builder.TurnContext.SendActivityAsync(String textReplyToSend, String speak, String inputHint, CancellationToken cancellationToken)\r\n at SemanticServices.AdapterWithErrorHandler.<>c__DisplayClass0_0.<<-ctor>b__0>d.MoveNext() in C:\\Users\\PC\\source\\repos\\SEKAI\\SEKAI\\AdapterWithErrorHandler.cs:line 28\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at Microsoft.Bot.Builder.BotAdapter.RunPipelineAsync(ITurnContext turnContext, BotCallbackHandler callback, CancellationToken cancellationToken)\r\n at Microsoft.Bot.Builder.BotFrameworkAdapter.ProcessActivityAsync(ClaimsIdentity claimsIdentity, Activity activity, BotCallbackHandler callback, CancellationToken cancellationToken)\r\n at Microsoft.Bot.Builder.BotFrameworkAdapter.ProcessActivityAsync(String authHeader, Activity activity, BotCallbackHandler callback, CancellationToken cancellationToken)\r\n at Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter.ProcessAsync(HttpRequest httpRequest, HttpResponse httpResponse, IBot bot, CancellationToken cancellationToken)\r\n at SemanticServices.Controllers.BotController.PostAsync() in C:\\Users\\PC\\source\\repos\\SEKAI\\SEKAI\\Controllers\\BotController.cs:line 33\r\n at lambda_method(Closure , Object )\r\n at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Logged|12_1(ControllerActionInvoker invoker)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)\r\n at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)\r\n at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)\r\n at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)\r\n\r\nHEADERS\r\n=======\r\nAccept: */*\r\nAccept-Encoding: gzip,deflate\r\nConnection: close\r\nContent-Length: 633\r\nContent-Type: application/json\r\nHost: localhost:3978\r\nUser-Agent: node-fetch/1.0 (+https://github.com/bitinn/node-fetch)\r\n"
}
So, your bot works when deployed on Azure, just not locally? Were you debug it locally and see if there's any reason why it would be trying to send back a null message? The fact that the error is in AdapterWithErrorHandler and the fact that it works when deployed seems to imply that it's a configuration issue.
Oh you are completly right! I had one credential wrong in my appsettings.json and thats why i couldnt test my bot locally. Okay, i can now test my bot locally via the Bot Framework Emulator and also when its deployed to Azure. But when i try to contact my deployed bot over the direct line API (with Postman) i still get the error message from the screenshots above. When i check the app insights, i get these informations:



System.ArgumentNullException: Value cannot be null. (Parameter 'textReplyToSend')
at Microsoft.Bot.Builder.TurnContext.SendActivityAsync(String textReplyToSend, String speak, String inputHint, CancellationToken cancellationToken)
at SemanticServices.AdapterWithErrorHandler.<>c__DisplayClass0_0.<<-ctor>b__0>d.MoveNext() in D:\a\1\s\SEKAI\AdapterWithErrorHandler.cs:line 28
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.Bot.Builder.BotAdapter.RunPipelineAsync(ITurnContext turnContext, BotCallbackHandler callback, CancellationToken cancellationToken)
at Microsoft.Bot.Builder.BotFrameworkAdapter.ProcessActivityAsync(ClaimsIdentity claimsIdentity, Activity activity, BotCallbackHandler callback, CancellationToken cancellationToken)
at Microsoft.Bot.Builder.BotFrameworkAdapter.ProcessActivityAsync(String authHeader, Activity activity, BotCallbackHandler callback, CancellationToken cancellationToken)
at Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter.ProcessAsync(HttpRequest httpRequest, HttpResponse httpResponse, IBot bot, CancellationToken cancellationToken)
at SemanticServices.Controllers.BotController.PostAsync() in D:\a\1\s\SEKAI\Controllers\BotController.cs:line 33
at lambda_method(Closure , Object )
at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Logged|12_1(ControllerActionInvoker invoker)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT1.ProcessRequestAsync()`

System.NullReferenceException: Object reference not set to an instance of an object.
at SemanticServices.Bots.SemanticServicesBot.DispatchToTopIntentAsync(ITurnContext1 turnContext, String intent, RecognizerResult recognizerResult, CancellationToken cancellationToken) in D:\a\1\s\SEKAI\Bots\SemanticServicesBot.cs:line 144
at SemanticServices.Bots.SemanticServicesBot.OnMessageActivityAsync(ITurnContext1 turnContext, CancellationToken cancellationToken) in D:\a\1\s\SEKAI\Bots\SemanticServicesBot.cs:line 87
at Microsoft.Bot.Builder.ActivityHandler.OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken)
at Microsoft.Bot.Builder.BotFrameworkAdapter.TenantIdWorkaroundForTeamsMiddleware.OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken)
at Microsoft.Bot.Builder.MiddlewareSet.ReceiveActivityWithStatusAsync(ITurnContext turnContext, BotCallbackHandler callback, CancellationToken cancellationToken)
at Microsoft.Bot.Builder.BotAdapter.RunPipelineAsync(ITurnContext turnContext, BotCallbackHandler callback, CancellationToken cancellationToken)
Is this still an error which occurs when i have saved the wrong access keys or could this be something else? I am asking because i am a bit confused that it is possible to chat with the bot over the GUI (which is direct line in the background) but not over an normal API Request with Postman.
I don't think this could be caused by your access credentials. Those would primarily affect your ability to connect to the bot, which appears to have been resolved. It looks like some kind of exception is being thrown in your bot, and the AdapterWithErrorHandler is catching the exception. You're getting a 500 error and a NullReferenceException on a message. This suggests that somewhere in your bot's code there is a bug that's causing the text for a message to be null.
I would suggest setting some breakpoints and stepping through the code to try and isolate where this exception is occurring. Once you know that, it should be a bit easier to fix.
Closing this issue due to inactivity. If you still require assistance, please comment and we can reopen the issue.