SIP REFER, NOTIFY 400 Bad request

Hello,

I have configured a simple extension:

[outbound-test-long]
exten => _.,1,NoOp(Outbound call to ${EXTEN})
 same => n,Dial(PJSIP/${EXTEN}@mytrunk,,U(after-answered-long))
exten => external_replaces,1,NoOp()
 same => n,Dial(PJSIP/default_outgoing/${SIPREFERTOHDR})

[after-answered-long]
exten => s,1,NoOp(Call anwered)
 same => n,Wait(4)
 ; play some audio
 same => n,Playback(sorry-youre-having-problems)
 same => n,Wait(40)
 same => n,Hangup()

I make a call and at some point the asterisk server receives a REFER request to transfer the call.
But asterisk returns 400 Bad request in one of the NOTIFY requests. Probably something is wrong with my configuration, but since there are no logs (even after enabling all asterisk logs) I have no idea what can be wrong. Can you help me please?

This is what I can see in the console, after I receive the REFER message

<--- Received SIP request (671 bytes) from TCP:34.63.9.192:46727 --->
REFER sip:asterisk@34.67.149.198:5060;transport=TCP SIP/2.0
Via: SIP/2.0/TCP 34.63.9.192:5060;branch=z9hG4bK.yJ6RPBBp7KilRplz;alias
Max-Forwards: 70
Call-ID: 6a562f38-e786-49a2-ba2e-66b528a7b854
Contact: <sip:34.63.9.192:5060;transport=tcp>
Refer-To: <sip:+19787634285@kabell.pstn.twilio.com>
Allow: INVITE, ACK, CANCEL, BYE, NOTIFY, REFER, MESSAGE, OPTIONS, INFO, SUBSCRIBE
X-Caller-ID: anonymous
X-Conversation-ID: conv_0601knat023mebdvrbb0h1zwrz46
Content-Length: 0
CSeq: 17506 REFER
From: <sip:+42117752580368620372784@sip.rtc.eleven2.dev>;tag=SCL_ojsjtHVfB6pH
To: "Anonymous" <sip:anonymous@anonymous.invalid>;tag=9e86c706-f7c6-46ff-bd50-85e4f99f7957


<--- Transmitting SIP response (654 bytes) to TCP:34.63.9.192:46727 --->
SIP/2.0 202 Accepted
Via: SIP/2.0/TCP 34.63.9.192:5060;rport=46727;received=34.63.9.192;branch=z9hG4bK.yJ6RPBBp7KilRplz;alias
Call-ID: 6a562f38-e786-49a2-ba2e-66b528a7b854
From: <sip:+42117752580368620372784@sip.rtc.eleven2.dev>;tag=SCL_ojsjtHVfB6pH
To: "Anonymous" <sip:anonymous@anonymous.invalid>;tag=9e86c706-f7c6-46ff-bd50-85e4f99f7957
CSeq: 17506 REFER
Expires: 600
Contact: <sip:asterisk@34.67.149.198:5060;transport=TCP>
Allow: OPTIONS, REGISTER, SUBSCRIBE, NOTIFY, PUBLISH, INVITE, ACK, BYE, CANCEL, UPDATE, PRACK, INFO, MESSAGE, REFER
Supported: 100rel, timer, replaces, norefersub
Server: Asterisk PBX 20.14.1
Content-Length:  0


<--- Transmitting SIP request (699 bytes) to TCP:34.63.9.192:5060 --->
NOTIFY sip:34.63.9.192:5060;transport=tcp SIP/2.0
Via: SIP/2.0/TCP 34.67.149.198:5060;rport;branch=z9hG4bKPj181759a1-18cf-4c21-982c-d5601289536d;alias
From: "Anonymous" <sip:anonymous@anonymous.invalid>;tag=9e86c706-f7c6-46ff-bd50-85e4f99f7957
To: <sip:+42117752580368620372784@sip.rtc.eleven2.dev>;tag=SCL_ojsjtHVfB6pH
Contact: <sip:asterisk@34.67.149.198:5060;transport=TCP>
Call-ID: 6a562f38-e786-49a2-ba2e-66b528a7b854
CSeq: 17506 NOTIFY
Event: refer
Subscription-State: active;expires=600
Allow-Events: message-summary, presence, dialog, refer
Max-Forwards: 70
User-Agent: Asterisk PBX 20.14.1
Content-Type: message/sipfrag;version=2.0
Content-Length:    20

SIP/2.0 100 Trying

<--- Transmitting SIP request (714 bytes) to TCP:34.63.9.192:5060 --->
NOTIFY sip:34.63.9.192:5060;transport=tcp SIP/2.0
Via: SIP/2.0/TCP 34.67.149.198:5060;rport;branch=z9hG4bKPj23308890-1784-4af1-b3c1-98965ec1b502;alias
From: "Anonymous" <sip:anonymous@anonymous.invalid>;tag=9e86c706-f7c6-46ff-bd50-85e4f99f7957
To: <sip:+42117752580368620372784@sip.rtc.eleven2.dev>;tag=SCL_ojsjtHVfB6pH
Contact: <sip:asterisk@34.67.149.198:5060;transport=TCP>
Call-ID: 6a562f38-e786-49a2-ba2e-66b528a7b854
CSeq: 17507 NOTIFY
Event: refer
Subscription-State: terminated;reason=noresource
Allow-Events: message-summary, presence, dialog, refer
Max-Forwards: 70
User-Agent: Asterisk PBX 20.14.1
Content-Type: message/sipfrag;version=2.0
Content-Length:    25

SIP/2.0 400 Bad Request

<--- Received SIP response (411 bytes) from TCP:34.63.9.192:5060 --->
SIP/2.0 200 success
Via: SIP/2.0/TCP 34.67.149.198:5060;rport=43661;branch=z9hG4bKPj181759a1-18cf-4c21-982c-d5601289536d;alias;received=34.67.149.198
From: "Anonymous" <sip:anonymous@anonymous.invalid>;tag=9e86c706-f7c6-46ff-bd50-85e4f99f7957
To: <sip:+42117752580368620372784@sip.rtc.eleven2.dev>;tag=SCL_ojsjtHVfB6pH
Call-ID: 6a562f38-e786-49a2-ba2e-66b528a7b854
CSeq: 17506 NOTIFY
Content-Length: 0


<--- Received SIP response (411 bytes) from TCP:34.63.9.192:5060 --->
SIP/2.0 200 success
Via: SIP/2.0/TCP 34.67.149.198:5060;branch=z9hG4bKPj23308890-1784-4af1-b3c1-98965ec1b502;alias;rport=43661;received=34.67.149.198
From: "Anonymous" <sip:anonymous@anonymous.invalid>;tag=9e86c706-f7c6-46ff-bd50-85e4f99f7957
To: <sip:+42117752580368620372784@sip.rtc.eleven2.dev>;tag=SCL_ojsjtHVfB6pH
Call-ID: 6a562f38-e786-49a2-ba2e-66b528a7b854
CSeq: 17507 NOTIFY
Content-Length: 0



The 400 is coming from whatever server the call is being referred to. You’ll need to identify how the Refer-To destination is routed and look at the SIP messages on that leg of the call.