PJSIP_HEADER(action,name[,number])
PJSIP_HEADER allows you to read specific SIP headers from the inbound PJSIP channel as well as write(add, update, remove) headers on the outbound channel. One exception is that you can read headers that you have already added on the outbound channel.
Examples:
``` title="Set somevar to the value of the From header" exten => 1,1,Set(somevar=${PJSIP_HEADER(read,From)})
``` title="Set via2 to the value of the 2nd Via header"
exten => 1,1,Set(via2=${PJSIP_HEADER(read,Via,2)})
``` title="Set xhdr to the value of the 1st X-header" exten => 1,1,Set(xhdr=${PJSIP_HEADER(read,X-*,1)})
``` title="Add an X-Myheader header with the value of myvalue"
exten => 1,1,Set(PJSIP_HEADER(add,X-MyHeader)=myvalue)
``` title="Add an X-Myheader header with an empty value" exten => 1,1,Set(PJSIP_HEADER(add,X-MyHeader)=)
``` title="Update the value of the header named X-Myheader to newvalue"
; 'X-Myheader' must already exist or the call will fail.
exten => 1,1,Set(PJSIP_HEADER(update,X-MyHeader)=newvalue)
``` title="Remove all headers whose names exactly match X-MyHeader" exten => 1,1,Set(PJSIP_HEADER(remove,X-MyHeader)=)
``` title="Remove all headers that begin with X-My"
exten => 1,1,Set(PJSIP_HEADER(remove,X-My*)=)
``` title="Remove all previously added headers" exten => 1,1,Set(PJSIP_HEADER(remove,*)=)
!!! note
The `remove` action can be called by reading **or** writing PJSIP_HEADER.
``` title="Display the number of headers removed"
exten => 1,1,Verbose( Removed ${PJSIP_HEADER(remove,X-MyHeader)} headers)
``` title="Set a variable to the number of headers removed" exten => 1,1,Set(count=${PJSIP_HEADER(remove,X-MyHeader)})
``` title="Just remove them ignoring any count"
exten => 1,1,Set(=${PJSIP_HEADER(remove,X-MyHeader)})
exten => 1,1,Set(PJSIP_HEADER(remove,X-MyHeader)=)
Note
If you call PJSIP_HEADER in a normal dialplan context you'll be operating on the caller's (incoming) channel which may not be what you want. To operate on the callee's (outgoing) channel call PJSIP_HEADER in a pre-dial handler.
``` title="Set headers on callee channel" [handler] exten => addheader,1,Set(PJSIP_HEADER(add,X-MyHeader)=myvalue) exten => addheader,2,Set(PJSIP_HEADER(add,X-MyHeader2)=myvalue2)
[somecontext] exten => 1,1,Dial(PJSIP/${EXTEN},,b(handler^addheader^1)) ```
actionrequiredread- Returns instance number of header name. A*may be appended to name to iterate over all headers beginning with name.add- Adds a new header name to this session.update- Updates instance number of header name to a new value. The header must already exist.remove- Removes all instances of previously added headers whose names match name. A*may be appended to name to remove all headers beginning with name. name may be set to a single*to clear all previously added headers. In all cases, the number of headers actually removed is returned.
namerequired - The name of the header.number- If there's more than 1 header with the same name, this specifies which header to read or update. If not specified, defaults to1meaning the first matching header. Not valid foraddorremove.
User Notes
No notes yet. Be the first to contribute a tip or example.
Contribute a note
Share a tip, gotcha, or practical example. Keep it under 2000 characters. No questions (use the Asterisk community forums for support). Wrap code in backticks.